openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
43,187 lines • 1.52 MB
JavaScript
import { i as testRegexWithBoundedInput } from "./safe-regex-tLlDZYfM.js";
import { t as compileConfigRegex } from "./config-regex-CvZFnWkO.js";
import "./redact-BDinS1q9.js";
import { a as hasErrnoCode, r as formatErrorMessage } from "./errors-BxyFnvP3.js";
import { a as registerUnhandledRejectionHandler } from "./unhandled-rejections-CDJ8dOVP.js";
import { a as getLogger, i as getChildLogger, o as getResolvedLoggerSettings } from "./logger-kwZIqwuw.js";
import { _ as resolveStateDir, i as isNixMode, l as resolveGatewayLockDir, o as resolveConfigPath, r as STATE_DIR, t as CONFIG_PATH, u as resolveGatewayPort } from "./paths-ViKUYWUK.js";
import "./tmp-openclaw-dir-idKIOMmb.js";
import { n as isRich, r as theme, t as colorize } from "./theme-CdOoMzRk.js";
import { a as logVerbose, s as setVerbose } from "./globals-DBUMOBZ8.js";
import { d as setConsoleTimestampPrefix, h as writeRuntimeJson, m as defaultRuntime, r as runtimeForLogger, t as createSubsystemLogger, u as setConsoleSubsystemFilter } from "./subsystem-DISldKSB.js";
import { t as sanitizeForLog } from "./ansi-BEJF8NKS.js";
import "./boolean-C3GkJetE.js";
import { n as logAcceptedEnvOption, t as isTruthyEnvValue } from "./env-Dnra1IpT.js";
import { n as resolveGlobalSingleton } from "./global-singleton-D9TlHTN5.js";
import "./warning-filter-CBhOcgHd.js";
import { C as sleep, D as isPlainObject, E as truncateUtf16Safe, S as shortenHomePath, c as ensureDir, l as escapeRegExp, r as clamp, y as resolveUserPath } from "./utils-CS0Ikux6.js";
import { t as formatDocsLink } from "./links-8xRhWBQL.js";
import { n as inheritOptionFromParent } from "./command-options-5coRiipK.js";
import { a as resolveOpenClawAgentDir } from "./paths-C4Oy7wjk.js";
import "./auth-profiles-DRuJBw5y.js";
import { d as normalizeUsageDisplay, f as normalizeVerboseLevel, l as normalizeReasoningLevel, n as formatXHighModelHint, o as normalizeElevatedLevel, s as normalizeFastMode, u as normalizeThinkLevel } from "./thinking.shared-BtwPxLYS.js";
import { n as resolveAgentModelPrimaryValue } from "./model-input-BB2wSAHb.js";
import { S as isCronRunSessionKey, T as parseAgentSessionKey, _ as normalizeAccountId$1, c as normalizeAgentId, g as DEFAULT_ACCOUNT_ID, h as toAgentStoreSessionKey, l as normalizeMainKey, m as toAgentRequestSessionKey, o as classifySessionKeyShape, r as buildAgentMainSessionKey, t as DEFAULT_AGENT_ID, u as resolveAgentIdFromSessionKey, v as normalizeOptionalAccountId, w as isSubagentSessionKey, x as isAcpSessionKey } from "./session-key-DAhnzjyr.js";
import { a as resolveAgentDir, b as matchesSkillFilter, f as resolveAgentSkillsFilter, i as resolveAgentConfig, m as resolveDefaultAgentId, n as listAgentEntries, p as resolveAgentWorkspaceDir, r as listAgentIds, u as resolveAgentModelFallbacksOverride, v as resolveSessionAgentId } from "./agent-scope-bjWqU22i.js";
import { o as isNotFoundPathError } from "./boundary-path-Dm0QJ7-y.js";
import { t as sameFileIdentity } from "./file-identity-D_ydQ7JC.js";
import { a as openVerifiedFileSync, i as openBoundaryFileSync, n as matchBoundaryFileOpenFailure, r as openBoundaryFile } from "./boundary-file-read-DcZxlWD8.js";
import { a as logWarn, t as logDebug } from "./logger-BmpSCz93.js";
import { n as runCommandWithTimeout, r as runExec } from "./exec-B5_AYfQG.js";
import { t as resolveOpenClawPackageRoot } from "./openclaw-root-DqTSxvim.js";
import { _ as resolveWorkspaceTemplateDir, a as DEFAULT_IDENTITY_FILENAME, c as DEFAULT_SOUL_FILENAME, d as ensureAgentWorkspace, g as resolveDefaultAgentWorkspaceDir, i as DEFAULT_HEARTBEAT_FILENAME, l as DEFAULT_TOOLS_FILENAME, o as DEFAULT_MEMORY_ALT_FILENAME, p as isWorkspaceSetupCompleted, r as DEFAULT_BOOTSTRAP_FILENAME, s as DEFAULT_MEMORY_FILENAME, t as DEFAULT_AGENTS_FILENAME, u as DEFAULT_USER_FILENAME } from "./workspace-D4K6QX9X.js";
import { n as DEFAULT_MODEL, r as DEFAULT_PROVIDER } from "./defaults-eEZJg44K.js";
import { S as resolveThinkingDefault, _ as resolveHooksGmailModel, a as getModelRefStatus, b as resolveSubagentConfiguredModelSelection, d as normalizeModelSelection, f as parseModelRef, g as resolveDefaultModelForAgent, h as resolveConfiguredModelRef, p as resolveAllowedModelRef, s as isCliProvider, t as buildAllowedModelSet, u as normalizeModelRef } from "./model-selection-BnFtDmP7.js";
import { A as applyConfigOverrides, At as applyMergePatch, Bt as resolveAgentMaxConcurrent, Lt as buildTalkConfigResponse, Mt as ensureControlUiAllowedOriginsForNonLoopbackBind, T as parseByteSize, Vt as resolveSubagentMaxConcurrent, c as parseConfigJson5, d as readConfigFileSnapshot, f as readConfigFileSnapshotForWrite, g as writeConfigFile, i as createConfigIO, jt as applyLegacyMigrations, p as resolveConfigSnapshotHash, s as loadConfig, u as readBestEffortConfig, x as validateConfigObjectWithPlugins, zt as resolveActiveTalkProviderConfig } from "./io-y3Az_Onx.js";
import "./shell-env-BOu7XeT_.js";
import "./safe-text-yavot2qw.js";
import { n as VERSION, o as resolveRuntimeServiceVersion } from "./version-CD3oP1-d.js";
import { a as hasConfiguredSecretInput } from "./types.secrets-DgX397vI.js";
import "./env-substitution-O6uabUmO.js";
import "./includes-Bj3eLUWH.js";
import "./zod-schema.providers-core-COy5nBQ0.js";
import "./legacy-web-search-BB-ZHEhz.js";
import { l as CHANNEL_IDS } from "./registry-C5UkPpaO.js";
import { a as normalizePluginsConfig, r as isTestDefaultMemorySlotDisabled } from "./config-state-Br0ucqMb.js";
import "./min-host-version-RMBWtIAR.js";
import "./manifest-registry-B5JNQdOM.js";
import "./runtime-guard-PhQ6PwQa.js";
import { a as isAvatarImageDataUrl, i as isAvatarHttpUrl, t as AVATAR_MAX_BYTES, u as looksLikeAvatarPath } from "./avatar-policy-B5nOfso_.js";
import "./ip-Ce8EDTBZ.js";
import { t as parseDurationMs } from "./parse-duration-D6oKXEgx.js";
import "./zod-schema.agent-runtime-Dtg4Jy6G.js";
import "./zod-schema.core-BuVz8Rk7.js";
import { n as resolveNormalizedAccountEntry, t as resolveAccountEntry } from "./account-lookup-C4CvUou_.js";
import { r as migrateLegacyConfig } from "./config-D4zN4BRl.js";
import { r as isPidAlive } from "./process-scoped-map-DAjtY4r-.js";
import "./file-lock-DEiVq2ow.js";
import "./audit-fs-DTQIjp2r.js";
import "./resolve-B7S4PlnO.js";
import { n as normalizeSecretInput } from "./normalize-secret-input-BsX1agd_.js";
import "./profiles-COPO-hHI.js";
import { t as formatCliCommand } from "./command-format-DBQUhADm.js";
import "./daemon-install-plan.shared-DlNoT_tA.js";
import "./runtime-paths-DOEOEFe-.js";
import "./gateway-install-token-De_pFluK.js";
import { i as enableTailscaleServe, n as disableTailscaleServe, o as getTailnetHostname, r as enableTailscaleFunnel, t as disableTailscaleFunnel } from "./tailscale-CxY-91GZ.js";
import { t as safeEqualSecret } from "./secret-equal-BFOmFk2p.js";
import { n as pickPrimaryTailnetIPv4, r as pickPrimaryTailnetIPv6 } from "./tailnet-DHiKSG7V.js";
import { c as normalizeHostHeader, d as resolveGatewayBindHost, f as resolveGatewayListenHosts, n as isLoopbackAddress, o as isTrustedProxyAddress, p as resolveRequestClientIp, r as isLoopbackHost, s as isValidIPv4, t as isLocalishHost, u as resolveClientIp } from "./net-JAg9zL0G.js";
import { a as resolveGatewayAuth, c as AUTH_RATE_LIMIT_SCOPE_SHARED_SECRET, i as isLocalDirectRequest, l as createAuthRateLimiter, n as authorizeHttpGatewayConnect, o as AUTH_RATE_LIMIT_SCOPE_DEVICE_TOKEN, r as authorizeWsControlUiGatewayConnect, s as AUTH_RATE_LIMIT_SCOPE_HOOK_AUTH, t as assertGatewayAuthConfigured, u as normalizeRateLimitClientIp } from "./auth-Buq0Niri.js";
import { d as readGatewayTokenEnv, u as readGatewayPasswordEnv } from "./credentials-D8pfRimu.js";
import { _ as normalizeGatewayClientMode, f as GATEWAY_CLIENT_CAPS, g as hasGatewayClientCap, h as GATEWAY_CLIENT_NAMES, i as isGatewayMessageChannel, l as normalizeMessageChannel, m as GATEWAY_CLIENT_MODES, n as isDeliverableMessageChannel, p as GATEWAY_CLIENT_IDS, r as isGatewayCliClient, s as isWebchatClient, t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-BliByQBl.js";
import { C as normalizeSessionDeliveryFields, O as snapshotSessionOrigin, l as updateSessionStore, n as loadSessionStore, s as saveSessionStore, t as archiveRemovedSessionTranscripts, u as updateSessionStoreEntry, v as deliveryContextFromSession, x as mergeDeliveryContext } from "./store-C5UK26Ce.js";
import { a as releasePinnedPluginHttpRouteRegistry, c as resolveActivePluginHttpRouteRegistry, i as pinActivePluginHttpRouteRegistry, t as getActivePluginRegistry, u as createEmptyPluginRegistry } from "./runtime-Iz8uZ7EU.js";
import { c as listChannelPlugins, l as normalizeChannelId, s as getChannelPlugin } from "./plugins-B09-vgme.js";
import { i as resolveMainSessionKey, n as resolveAgentMainSessionKey, r as resolveExplicitAgentSessionKey, t as canonicalizeMainSessionAlias } from "./main-session-ClbmrpGK.js";
import { _ as resolveSessionResetPolicy, b as resolveMainSessionKeyFromConfig, d as emitSessionTranscriptUpdate, f as onSessionTranscriptUpdate, h as evaluateSessionFreshness, i as resolveAgentSessionDirs, l as extractDeliveryInfo, u as parseSessionThreadInfo } from "./sessions-D9ZgHCHb.js";
import { a as resolveSessionTranscriptPath, c as resolveSessionTranscriptsDirForAgent, i as resolveSessionFilePathOptions, l as resolveStorePath, r as resolveSessionFilePath } from "./paths-rhN9LKM_.js";
import { n as mergeSessionEntry, o as setSessionRuntimeModel } from "./types-BRBQbfEQ.js";
import { n as cleanStaleLockFiles } from "./session-write-lock-va3qZE6f.js";
import { n as readJsonFile, r as writeJsonAtomic, t as createAsyncLock } from "./json-files-2-D1jkJB.js";
import { $ as validateExecApprovalsNodeSetParams, $t as errorShape, A as validateConfigPatchParams, At as validateSessionsPatchParams, B as validateCronRunsParams, Bt as validateTalkConfigParams, C as validateChannelsStatusParams, Ct as validateSessionsAbortParams, D as validateChatSendParams, Dt as validateSessionsListParams, E as validateChatInjectParams, Et as validateSessionsDeleteParams, F as validateConnectParams, Ft as validateSessionsUsageParams, G as validateDevicePairRejectParams, Gt as validateWakeParams, H as validateCronUpdateParams, Ht as validateTalkSpeakParams, I as validateCronAddParams, It as validateSkillsBinsParams, J as validateDeviceTokenRotateParams, Jt as validateWizardCancelParams, K as validateDevicePairRemoveParams, Kt as validateWebLoginStartParams, L as validateCronListParams, Lt as validateSkillsInstallParams, M as validateConfigSchemaLookupResult, Mt as validateSessionsResetParams, N as validateConfigSchemaParams, Nt as validateSessionsResolveParams, O as validateConfigApplyParams, Ot as validateSessionsMessagesSubscribeParams, P as validateConfigSetParams, Pt as validateSessionsSendParams, Q as validateExecApprovalsNodeGetParams, Qt as ErrorCodes, R as validateCronRemoveParams, Rt as validateSkillsStatusParams, S as validateChannelsLogoutParams, St as validateSendParams, T as validateChatHistoryParams, Tt as validateSessionsCreateParams, U as validateDevicePairApproveParams, Ut as validateToolsCatalogParams, V as validateCronStatusParams, Vt as validateTalkModeParams, W as validateDevicePairListParams, Wt as validateUpdateRunParams, X as validateExecApprovalResolveParams, Xt as validateWizardStartParams, Y as validateExecApprovalRequestParams, Yt as validateWizardNextParams, Z as validateExecApprovalsGetParams, Zt as validateWizardStatusParams, _ as validateAgentsFilesGetParams, _t as validatePollParams, a as READ_SCOPE, an as resolveAuthConnectErrorDetailCode, at as validateNodeInvokeParams, b as validateAgentsListParams, bt as validateSecretsResolveParams, c as isNodeRoleMethod, cn as buildDeviceAuthPayloadV3, ct as validateNodePairApproveParams, d as formatValidationErrors, dn as loadOrCreateDeviceIdentity, dt as validateNodePairRequestParams, en as parseSessionLabel, et as validateExecApprovalsSetParams, f as validateAgentIdentityParams, fn as normalizeDevicePublicKeyBase64Url, ft as validateNodePairVerifyParams, g as validateAgentsDeleteParams, gt as validateNodeRenameParams, h as validateAgentsCreateParams, hn as verifyDeviceSignature, ht as validateNodePendingEnqueueParams, i as PAIRING_SCOPE, in as ConnectErrorDetailCodes, it as validateNodeEventParams, j as validateConfigSchemaLookupParams, jt as validateSessionsPreviewParams, k as validateConfigGetParams, kt as validateSessionsMessagesUnsubscribeParams, lt as validateNodePairListParams, m as validateAgentWaitParams, mt as validateNodePendingDrainParams, n as APPROVALS_SCOPE, nt as validateModelsListParams, o as WRITE_SCOPE, on as resolveDeviceAuthConnectErrorDetailCode, ot as validateNodeInvokeResultParams, p as validateAgentParams, pn as publicKeyRawBase64UrlFromPem, pt as validateNodePendingAckParams, q as validateDeviceTokenRevokeParams, qt as validateWebLoginWaitParams, rn as normalizeInputProvenance, rt as validateNodeDescribeParams, s as authorizeOperatorScopesForMethod, sn as buildDeviceAuthPayload, st as validateNodeListParams, t as ADMIN_SCOPE$2, tt as validateLogsTailParams, un as deriveDeviceIdFromPublicKey, ut as validateNodePairRejectParams, v as validateAgentsFilesListParams, vt as validatePushTestParams, w as validateChatAbortParams, wt as validateSessionsCompactParams, x as validateAgentsUpdateParams, xt as validateSecretsResolveResult, y as validateAgentsFilesSetParams, yt as validateRequestFrame, z as validateCronRunParams, zt as validateSkillsUpdateParams } from "./method-scopes-BiEi0X2g.js";
import { d as loadGatewayTlsRuntime$1, r as callGateway } from "./call-CQbSO4Fr.js";
import { n as normalizeDeviceAuthScopes } from "./device-auth-DUVzz7ar.js";
import { t as rawDataToString } from "./ws-B_hsocKt.js";
import { t as normalizeDeviceMetadataForAuth } from "./device-metadata-normalization-Mvq74l2j.js";
import { i as resolveAssistantAvatarUrl, n as buildControlUiAvatarUrl, r as normalizeControlUiBasePath, t as CONTROL_UI_AVATAR_PREFIX } from "./control-ui-shared-BBhZvISc.js";
import { S as pickBestEffortPrimaryLanIPv4, c as handleReset, x as inspectBestEffortPrimaryTailnetIPv4 } from "./onboard-helpers-D5cfC-Ra.js";
import "./prompt-style-C4CV4Y_p.js";
import "./ports-lsof-BLEgL_Gn.js";
import { t as cleanStaleGatewayProcessesSync } from "./restart-stale-pids-BsPvlGJd.js";
import "./runtime-parse-BUn1nl6s.js";
import { p as scheduleDetachedLaunchdRestartHandoff } from "./launchd-yOx8f787.js";
import { c as parseProcCmdline, s as isGatewayArgv } from "./service-DdJ2Qk7P.js";
import { a as inspectPortUsage, s as formatPortDiagnostics } from "./ports-FldL_Lhq.js";
import "./systemd-bGhJCG6J.js";
import "./shared-CaPOEZsR.js";
import "./systemd-hints-DvBn5bxv.js";
import "./logging-CznuqaMu.js";
import { t as parsePort } from "./parse-port-BdJPfG31.js";
import { n as addGatewayServiceCommands } from "./daemon-cli-CqiqYyPe.js";
import { r as isRestartEnabled } from "./commands-C6oAw_H-.js";
import { i as probeGateway } from "./probe-BPU90Y9V.js";
import { n as formatConfigIssueLines } from "./issue-format-BtLIwOtN.js";
import "./lifecycle-core-DUBuppms.js";
import "./status-B6f3xZ42.js";
import "./diagnostics-CwDNepDU.js";
import "./inspect-CWK9qVB8.js";
import "./probe-auth-dhNhn0B4.js";
import { n as withProgress } from "./progress-C78aA91n.js";
import { t as formatHelpExamples } from "./help-format-C_5MeE_w.js";
import "./memory-cli-C16LjAdf.js";
import { n as resolveAgentIdentity, r as resolveEffectiveMessagesConfig } from "./identity-Dp-UIak4.js";
import { a as stripHeartbeatToken, i as resolveHeartbeatPrompt$1, l as isSilentReplyText, o as HEARTBEAT_TOKEN, r as isHeartbeatContentEffectivelyEmpty, s as SILENT_REPLY_TOKEN } from "./heartbeat-Bdfr_onc.js";
import { $d as markGatewaySigusr1RestartHandled, $l as registerAgentRunContext, $y as resolveExecApprovalCommandDisplay, Af as closeTrackedBrowserTabsForSessions, As as resolveSessionModelRef, Bd as stripInlineDirectiveTagsFromMessageForDisplay, Bf as clearSecretsRuntimeSnapshot, Bo as resolveToolLoopDetectionConfig, Bs as stripEnvelopeFromMessages, Bv as getTotalPendingReplies, Cs as listAgentsForGateway, Ct as withPluginRuntimeGatewayRequestScope, Cu as getQueueSize, Dm as isTtsProviderConfigured, Ds as loadSessionEntry, Du as setCommandLaneConcurrency, Em as isTtsEnabled, Es as loadGatewaySessionRow, Eu as resetAllLanes, FS as extractTextFromChatContent, Fd as resetDirectoryCache, Fm as resolveTtsProviderOrder, Fp as missingSystemRunApprovalBinding, Fs as cleanupArchivedSessionTranscripts, Ft as resolveWhatsAppAccount, Fu as normalizeGroupActivation, Fy as emitHeartbeatEvent, GS as resolveCronStyleNow, Gl as resolveAnnounceTargetFromKey, Gm as getSpeechProvider, Go as resolveGroupToolPolicy, Gp as resolveHeartbeatReasonKind, Gv as getGlobalHookRunner, Gy as resolveOutboundTarget, Hf as prepareSecretsRuntimeSnapshot, Hl as replaceSubagentRunAfterSteer, Hm as synthesizeSpeech, Ho as buildDefaultToolPolicyPipelineSteps, Hp as requestHeartbeatNow, Hy as resolveExecApprovalSessionTarget, Id as resolveMessageChannelSelection, Il as getSubagentRunByChildSessionKey, Is as readSessionMessages, Iy as getLastHeartbeatEvent, Jd as resolveFailoverReasonFromError, Jl as onSessionLifecycleEvent, Jo as createOpenClawTools, Jy as applyPluginAutoEnable, Kl as readLatestAssistantReply, Km as listSpeechProviders, Ko as resolveSubagentToolPolicy, Ky as resolveSessionDeliveryTarget, Ll as initSubagentRegistry, Lp as toSystemRunApprovalMismatchError, Ls as readSessionPreviewItemsFromTranscript, Ly as onHeartbeatEvent, Mf as persistBrowserProxyFiles, Mm as resolveTtsAutoMode, Mp as buildSystemRunApprovalBinding, Ms as archiveSessionTranscripts, Nl as countActiveDescendantRuns, Nm as resolveTtsConfig, Np as buildSystemRunApprovalEnvBinding, Ns as attachOpenClawTranscriptMeta, Op as resolveSystemRunApprovalRequestContext, Os as migrateAndPruneGatewaySessionStoreKey, Ou as waitForActiveTasks, Pd as maybeResolveIdLikeTarget, Pm as resolveTtsPrefsPath, Pp as matchSystemRunApprovalBinding, Ps as capArrayByJsonBytes, Pu as resolveHeartbeatReplyPayload, Pv as dispatchInboundMessage, Py as resolveHeartbeatVisibility, Qd as isGatewaySigusr1RestartExternallyAllowed, Ql as onAgentEvent, Rs as resolveSessionTranscriptCandidates, Rv as createReplyDispatcher, Sd as deliverOutboundPayloads, St as getPluginRuntimeGatewayRequestScope, Su as getActiveTaskCount, Ts as loadCombinedSessionStoreForGateway, Tu as markGatewayDraining, US as ensureOpenClawModelsJson, Uf as resolveCommandSecretsFromActiveRuntimeSnapshot, Um as textToSpeech, Up as setHeartbeatWakeHandler, Uv as normalizeSendPolicy, Uy as resolveHeartbeatDeliveryTarget, Vf as getActiveSecretsRuntimeSnapshot, Vm as setTtsProvider, Vo as applyToolPolicyPipeline, Vp as areHeartbeatsEnabled, WS as appendCronStyleCurrentTimeLine, Wd as runWithModelFallback, Wo as resolveEffectiveToolPolicy, Wp as setHeartbeatsEnabled, Wv as resolveSendPolicy, Wy as resolveHeartbeatSenderContext, Xd as deferGatewayRestartUntilIdle, Xl as emitAgentEvent, Xm as OPENAI_TTS_MODELS, Xp as getPluginToolMeta, Xv as runGlobalGatewayStopSafely, Yd as consumeGatewaySigusr1RestartAuthorization, Yl as clearAgentRunContext, Zd as emitGatewayRestart, Zl as getAgentRunContext, Zm as OPENAI_TTS_VOICES, Zp as resolvePluginTools, _f as normalizeOptionalText$1, _m as resolveModelCostConfig, _p as resolveFastModeState, _s as loadProviderUsageSummary, a as isEmbeddedPiRunActive, aa as createOutboundSendDeps, af as formatDoctorNonInteractiveHint, au as getReplyFromConfig, av as deriveSessionTotalTokens, bc as handleSlackHttpRequest, bp as evaluateGatewayAuthSurfaceStates, c as waitForActiveEmbeddedRuns, ca as runCliAgent, cf as summarizeRestartSentinel, da as createOutboundSendDeps$1, df as normalizeHttpWebhookUrl, eb as sanitizeExecApprovalDisplayText, ec as parseTimeoutMsWithFallback, ef as scheduleGatewaySigusr1Restart, em as clearBootstrapSnapshot, es as killControlledSubagentRun, ff as normalizeCronJobCreate, fs as normalizeSpawnedRunMetadata, gf as normalizeOptionalSessionKey, gm as formatUsd, gs as resolvePreferredSessionKeyForSessionIdMatches, hf as normalizeOptionalAgentId, hm as formatTokenCount, ht as loadOpenClawPlugins, iC as getMachineDisplayName, ia as resolveAgentOutboundTarget, ib as getChannelActivity, if as consumeRestartSentinel, ih as resolveModelAsync, im as resolveBootstrapWarningSignaturesSeen, jf as applyBrowserProxyPaths, jl as stopSubagentsForRequester, jm as resolveTtsApiKey, jp as resolveSystemRunCommandRequest, js as archiveFileOnDisk, kp as resolveSystemRunApprovalRuntimeContext, ks as resolveGatewaySessionStoreTarget, ku as CommandLane, l as waitForEmbeddedPiRunEnd, mm as estimateUsageCost, mu as buildBareSessionResetPrompt, n as abortEmbeddedPiRun, nc as ensureOutboundSessionEntry, nf as setPreRestartDeferralCheck, ns as resolveSubagentController, oa as getCliSessionId, of as formatRestartSentinelMessage, ov as hasNonzeroUsage, pf as normalizeCronJobPatch, ps as resolveIngressWorkspaceOverrideForSpawnedRun, ql as resolveNestedAgentLane, qm as normalizeSpeechProviderId, qo as runBeforeToolCallHook, qs as jsonUtf8Bytes, qy as resolveOutboundChannelPlugin, r as getActiveEmbeddedRunCount, ra as resolveAgentDeliveryPlan, rc as resolveOutboundSessionRoute, rf as triggerOpenClawRestart, sa as setCliSessionId, t as runEmbeddedPiAgent, ta as agentCommandFromIngress, tf as setGatewaySigusr1RestartPolicy, tm as clearBootstrapSnapshotOnSessionRollover, ts as killSubagentRunAdmin, tu as clearSessionQueues, ua as createDefaultDeps, uf as writeRestartSentinel, v as rewriteTranscriptEntriesInSessionFile, vf as normalizePayloadToSystemText, vm as startGatewayModelPricingRefresh, vx as normalizeWhatsAppTarget, wd as normalizeReplyPayloadsForDelivery, wm as getTtsProvider, ws as listSessionsFromStore, wu as getTotalQueueSize, xd as buildOutboundSessionContext, xf as normalizeLegacyDeliveryInput, xs as canonicalizeSpawnedByForAgent, xu as enqueueCommandInLane, xv as isAbortRequestText, yf as normalizeRequiredName, yp as GATEWAY_AUTH_SURFACE_PATHS, zS as loadModelCatalog, zd as stripInlineDirectiveTagsForDisplay, zf as activateSecretsRuntimeSnapshot, zl as listDescendantRunsForRequester, zm as setTtsEnabled, zs as stripEnvelopeFromMessage, zy as resolveIndicatorType } from "./pi-embedded-CbCYZxIb.js";
import { d as triggerInternalHook, n as createInternalHookEvent, t as clearInternalHooks, u as registerInternalHook } from "./internal-hooks-CIUghoK4.js";
import "./multimodal-B8yEZcl5.js";
import { t as resolveMemorySearchConfig } from "./memory-search-B_aq08y5.js";
import "./provider-catalog-rVN7ZbR5.js";
import "./secret-input-DXoOLNgb.js";
import { t as buildChannelAccountBindings } from "./bindings-CFudGB9Z.js";
import "./resolve-route-DwFWP4JW.js";
import "./routing-zOQ0wwtV.js";
import { i as resolveUserTimezone } from "./date-time-DcBRR7Wo.js";
import "./identity-file-DVLib087.js";
import { i as resolveAgentAvatar, n as resolveAgentOutboundIdentity } from "./outbound-runtime-m-y0nd6F.js";
import "./provider-env-vars-CpUJqT5v.js";
import "./provider-auth-input-Bus9W_qh.js";
import "./provider-model-minimax-DSmX4BNO.js";
import "./provider-models-BKjzpTsb.js";
import "./anthropic-vertex-provider-B2x4YywE.js";
import "./provider-catalog-BTgF8VyO.js";
import "./provider-catalog-D0IlX18O.js";
import "./provider-catalog-BTmUX0Mp.js";
import "./provider-catalog-sph_Rl63.js";
import "./provider-catalog-C7tUyBwV.js";
import "./provider-catalog-C8dPZpHW.js";
import "./provider-catalog-BJNff1VI.js";
import "./provider-catalog-IAdymoqT.js";
import "./provider-catalog-Bjc7evzh.js";
import "./models-config.providers.discovery-CjkO08q6.js";
import "./text-runtime-DVXt5xhi.js";
import { i as listCoreToolSections, n as PROFILE_OPTIONS, o as resolveCoreToolProfiles } from "./tool-catalog-BjSY4C4F.js";
import { G as resolveToolProfilePolicy, V as mergeAlsoAllowPolicy, z as collectExplicitAllowlist } from "./docker-Bhjg8g2t.js";
import "./sandbox-DTlKNieF.js";
import { T as estimateBase64DecodedBytes, r as ToolInputError } from "./common-zReYuoMM.js";
import "./image-ops-By4YfyWa.js";
import { i as supportsXHighThinking, t as formatThinkingLevels } from "./thinking-CJoSRIGR.js";
import { n as assertNoPathAliasEscape } from "./path-alias-guards-CwRM04O1.js";
import "./sandbox-paths-Cn8i8Jxm.js";
import "./channel-actions-fV93bLjV.js";
import { t as detectMime } from "./mime-CsUtzcn2.js";
import { t as SsrFBlockedError } from "./ssrf-0bPJMoZR.js";
import { n as fetchWithSsrFGuard } from "./fetch-guard-CYl1q2XH.js";
import "./provider-web-search-COZNygbd.js";
import { t as getAcpSessionManager, x as resolveAgentTimeoutMs } from "./manager-Bw8JrihM.js";
import "./config-schema-CPzP2Ds6.js";
import "./channel-plugin-common-ClptUZHU.js";
import { i as resolveChannelDefaultAccountId } from "./helpers-BhHi_aZN.js";
import "./status-helpers-Cflmo90d.js";
import { h as primeConfiguredBindingRegistry } from "./conversation-runtime-nmWgJSXS.js";
import "./runtime-whatsapp-boundary-XjdkYMz_.js";
import { o as readChannelAllowFromStoreSync } from "./pairing-store-BGKLrjo6.js";
import "./json-store--JDX3zNL.js";
import "./secret-file-Bif7RlZ-.js";
import "./channel-config-schema-CgpS17zB.js";
import "./setup-binary-C2MYZTCc.js";
import { d as isWithinDir } from "./archive-pOxyr9r4.js";
import { a as openFileWithinRoot, c as readLocalFileSafely, f as writeFileWithinRoot, t as SafeOpenError } from "./fs-safe-DZyOgpPN.js";
import "./signal-cli-install-fVOL9FvX.js";
import "./setup-wizard-proxy-DoOpOF8S.js";
import "./setup-BI3ohdrT.js";
import "./dm-policy-shared-DtcQProI.js";
import "./runtime-env-DQOYrXVl.js";
import "./temp-path-B91lt0LF.js";
import { n as isDiagnosticsEnabled } from "./diagnostic-events-DLiZiw1x.js";
import { _ as startDiagnosticHeartbeat, v as stopDiagnosticHeartbeat } from "./diagnostic-CDTtMQBT.js";
import "./logging-CCTKg_fh.js";
import { n as resolveCronStorePath, r as saveCronStore, t as loadCronStore } from "./config-runtime-BYNizC50.js";
import { t as applyModelOverrideToSessionEntry } from "./model-overrides-B_s4f-Dt.js";
import { c as resolveSessionAuthProfileOverride, f as computeBackoff, l as lookupContextTokens, n as parseVerboseOverride, p as sleepWithAbort, t as applyVerboseOverride } from "./level-overrides-DzIzQ3ZY.js";
import { b as saveExecApprovals, c as normalizeExecApprovals, f as readExecApprovalsSnapshot, o as mergeExecApprovalsSocketDefaults, r as ensureExecApprovals, t as DEFAULT_EXEC_APPROVAL_TIMEOUT_MS } from "./exec-approvals-BF_Qfdq8.js";
import { I as requestBodyErrorToText, P as readJsonBodyWithLimit, d as PROTECTED_PLUGIN_ROUTE_PREFIXES, f as canonicalizePathForSecurity, p as canonicalizePathVariant } from "./webhook-ingress-D-H5frou.js";
import { a as isSystemEventContextChanged, o as peekSystemEventEntries, r as enqueueSystemEvent } from "./system-events-BU_NYynX.js";
import "./ssrf-policy-DYINyIKC.js";
import { p as resolveSendableOutboundReplyParts, s as hasOutboundReplyContent } from "./reply-payload-CiQ7TPgp.js";
import "./hook-runtime-BUYmDbaA.js";
import "./templating-BDXRiJUh.js";
import { t as createChannelReplyPipeline } from "./channel-reply-pipeline-BxH4-AP2.js";
import { n as formatZonedTimestamp } from "./format-datetime-C-Bvpmjp.js";
import { r as buildHistoryContextFromEntries } from "./reply-history-n7nOPLta.js";
import "./commands-registry.data-BFXJWoqi.js";
import "./commands-registry-Bh9pJ817.js";
import { f as hasBinary } from "./frontmatter-CP5371Rm.js";
import "./env-overrides-BNSo89u5.js";
import { o as loadWorkspaceSkillEntries, r as buildWorkspaceSkillSnapshot } from "./skills-M0AZJeXx.js";
import { _ as registerSkillsChangeListener, a as refreshRemoteNodeBins, c as approveNodePairing, d as renamePairedNode, f as requestNodePairing, g as getSkillsSnapshotVersion, i as refreshRemoteBinsForConnectedNodes, l as listNodePairing, m as verifyNodeToken, n as primeRemoteSkillsCache, o as removeRemoteNodeInfo, p as updatePairedNodeMetadata, r as recordRemoteNodeInfo, s as setSkillsRemoteRegistry, t as getRemoteSkillEligibility, u as rejectNodePairing } from "./skills-remote-BHxDA4Jw.js";
import { t as listAgentWorkspaceDirs } from "./workspace-dirs-DqDLiRvZ.js";
import "./pairing-token-DAszGmwD.js";
import "./skill-commands-D73dGR_l.js";
import "./config-CLRQPUSt.js";
import { A as cleanOldMedia, P as saveMediaBuffer, _ as startBrowserControlServiceFromConfig, g as createBrowserControlContext, h as createBrowserRouteDispatcher, it as resolveConfiguredSecretInputString, nt as mergeGatewayAuthConfig, rt as mergeGatewayTailscaleConfig, tt as ensureGatewayStartupAuth, x as movePathToTrash } from "./routes-XT2JfvT-.js";
import "./ssh-tunnel-LM52qY6N.js";
import "./server-middleware-BP59C6FL.js";
import "./provider-auth-ref-CQEp9sp3.js";
import "./provider-auth-helpers-Do3RFvdi.js";
import "./provider-api-key-auth-BEeRW3dq.js";
import "./pairing-labels-D27Y4Vv6.js";
import "./directory-runtime-D9Y42mW-.js";
import "./read-only-account-inspect-CJPRBFj7.js";
import "./src-BoNU6eQs.js";
import "./web-media-PKy3RbRG.js";
import "./media-understanding-BDOSvuM9.js";
import { n as normalizePollInput } from "./polls-BSvCQ2Fu.js";
import "./web-media-CiSbdGPs.js";
import "./state-paths-Dvb3Mfl3.js";
import "./llm-task-Br6Ixjlh.js";
import "./pi-model-discovery-DO3tFksS.js";
import "./exec-inline-eval-CpmxWJsW.js";
import { i as isKnownSecretTargetId } from "./target-registry-9OUOvlxj.js";
import { a as mapHookExternalContentSource, i as isExternalHookSession, n as detectSuspiciousPatterns, o as resolveHookExternalContentSource, t as buildSafeExternalPrompt } from "./external-content-DZI9vlJn.js";
import "./brave-onFD06fq.js";
import "./duckduckgo-CZuAOfiQ.js";
import "./exa-5bwgY04k.js";
import "./security-runtime-xlw0Fxx2.js";
import "./provider-usage-ClxTGn5c.js";
import "./provider-models-Bjv0V4OP.js";
import "./media-understanding-provider-DllGbg-v.js";
import "./media-understanding-provider-BDq4rFL-.js";
import "./provider-onboard-CSnPvkMM.js";
import "./onboard-BU8aRvq7.js";
import "./perplexity-web-search-provider-CI38RNUU.js";
import "./perplexity-CMZbsreI.js";
import "./model-definitions-CLagLj0g.js";
import "./onboard-0P7GBI2D.js";
import "./provider-catalog-CKHo9CRM.js";
import "./provider-models-Dmjtw6kn.js";
import "./stream-C-KGzzT0.js";
import "./web-search-CO1hvu_l.js";
import { i as parseAbsoluteTimeMs, n as resolveCronStaggerMs, r as resolveDefaultCronStaggerMs, t as normalizeCronStaggerMs } from "./stagger-CFqdtCYL.js";
import "./command-secret-targets-DLs8jC2H.js";
import "./delivery-queue-DHURMSqX.js";
import "./channel-summary-Ctrv7moa.js";
import "./follow-redirects-CVAaqIk4.js";
import "./tool-policy-match-DQWWRSN4.js";
import "./runtime-CAdCdG-M.js";
import "./channel-status-BfM90KqH.js";
import "./discord-core-DLbIyXWy.js";
import "./cli-runtime-Cx1v7N7v.js";
import "./config-presence-DA3j8Nmi.js";
import "./query-expansion-BOJBTXsp.js";
import { n as getMemorySearchManager, r as resolveMemoryBackendConfig } from "./search-manager-DNBHwaee.js";
import "./acp-runtime-BHZj-SLU.js";
import "./telegram-core-DD8gCuaX.js";
import "./audit-BI48Fi3j.js";
import "./gateway-runtime-Dn2qAGY7.js";
import "./connection-auth-Nl2a3tDb.js";
import "./mcp-config-CDIWSqVk.js";
import { n as createPluginRuntime, r as setGatewaySubagentRuntime } from "./runtime-DQb3-ECx.js";
import "./audit-86GvYypd.js";
import "./runtime-api-Ckhst1XP.js";
import "./route-resolution-C9XDTDsW.js";
import "./thread-bindings.messages-C6WRAx05.js";
import "./provider-kAmP8t4k.js";
import "./timeouts-BnOxp0oI.js";
import "./message-handler-DmHqPMDi.js";
import "./thread-bindings-neFAVWsT.js";
import "./ui-BU9Tv965.js";
import "./src-VVAfbRUn.js";
import "./resolve-users-D9S1NRZ1.js";
import "./resolve-channels-Cx3FRWkT.js";
import "./timeouts-DKbQkIWS.js";
import "./discord-DYsL5ZlK.js";
import "./imessage-BRjuIPul.js";
import "./process-runtime-2HF_sK3e.js";
import { t as WizardCancelledError } from "./prompts-BEHcWT6j.js";
import "./run-command-DPUsJhO7.js";
import "./channel-setup-C5JbULY6.js";
import "./setup-tools-CYv8hVOc.js";
import "./ssrf-runtime-BT3ekdqD.js";
import "./backup-create-BuxXJpr7.js";
import "./matrix-wu3iTLk7.js";
import "./helper-api-CFToxS1G.js";
import "./setup-group-access-BIiwXi_V.js";
import "./runtime-api-C0mTuF54.js";
import "./matrix-migration-snapshot-DTfN6Lm9.js";
import { a as rejectDevicePairing, c as revokeDeviceToken, d as updatePairedDeviceMetadata, f as verifyDeviceToken, i as listDevicePairing, l as rotateDeviceToken, m as roleScopesAllow, n as ensureDeviceToken, o as removePairedDevice, p as resolveMissingRequestedScope, r as getPairedDevice, s as requestDevicePairing, t as approveDevicePairing, u as summarizeDeviceTokens } from "./device-pairing-CcKdnVxY.js";
import { i as verifyDeviceBootstrapToken } from "./device-bootstrap-X34uUqGh.js";
import "./whatsapp-core-BTUSucnn.js";
import "./active-listener-Dbfg0fFk.js";
import "./channel.runtime-Cu2TxQlg.js";
import "./session-BFZksknu.js";
import "./agent-DtXXgp1u.js";
import "./frontmatter-AzDGezO7.js";
import { n as shouldIncludeHook } from "./config-Bnl_ZCTo.js";
import { t as buildWorkspaceSkillStatus } from "./skills-status-BhDjqqQc.js";
import "./install-source-utils-C3NuFv6M.js";
import "./clawhub-CrF94Azs.js";
import "./status-DI5hmNdJ.js";
import { a as loadSessionUsageTimeSeries, n as loadCostUsageSummary, o as resolveExistingUsageSessionFile, r as loadSessionCostSummary, t as discoverAllSessions } from "./session-cost-usage-BtumKIx6.js";
import { a as extractImageContentFromSource, c as resolveInputFileLimits, i as extractFileContentFromSource, l as renderFileContextBlock, n as DEFAULT_INPUT_IMAGE_MIMES, o as normalizeMimeList, r as DEFAULT_INPUT_TIMEOUT_MS, t as DEFAULT_INPUT_IMAGE_MAX_BYTES } from "./input-files-USwyDyYr.js";
import { t as ensureOpenClawCliOnPath } from "./path-env-BjkQG0S1.js";
import { n as DEFAULT_GATEWAY_HTTP_TOOL_DENY } from "./dangerous-tools-DPr4YFtb.js";
import { t as readSecretFromFile } from "./secret-file-BoJkIcer.js";
import { n as runCommandWithRuntime } from "./cli-utils-C5ijQCJ_.js";
import { t as buildChannelUiCatalog } from "./catalog-CgTRyvtW.js";
import { a as applyDerivedTags, i as applySensitiveHints, n as redactConfigSnapshot, r as restoreRedactedValues, t as redactConfigObject } from "./redact-snapshot-B_3oaPCI.js";
import "./table-7g_hocqO.js";
import { n as resolveWideAreaDiscoveryDomain, r as writeWideAreaGatewayZone } from "./widearea-dns-ClYY9gAS.js";
import { i as toOptionString, n as extractGatewayMiskeys, r as maybeExplainGatewayServiceStop, t as describeUnknownError } from "./shared-DdWaoh8l.js";
import { i as discoverGatewayBeacons, n as buildGatewayDiscoveryTarget, r as serializeGatewayDiscoveryBeacon } from "./gateway-discovery-targets-BMX5B-8T.js";
import { h as pickGatewaySelfPresence, t as getStatusSummary } from "./status.summary-BK0AMFlQ.js";
import { n as resolveHeartbeatIntervalMs, t as isHeartbeatEnabledForAgent } from "./heartbeat-summary-B-oiNNxm.js";
import { i as styleHealthChannelLine, n as getHealthSnapshot, t as formatHealthChannelLines } from "./health-CSjz5K7z.js";
import { a as resolveControlUiRootOverrideSync, n as isPackageProvenControlUiRootSync, o as resolveControlUiRootSync, t as ensureControlUiAssetsBuilt } from "./control-ui-assets-DBgPQZLL.js";
import { _ as computePreviousRunAtMs, g as computeNextRunAtMs, h as coerceFiniteScheduleNumber, m as normalizeStoredCronJobs, n as runGatewayUpdate, t as runStartupMatrixMigration, v as detectPluginInstallPathIssue, y as formatPluginInstallPathIssue } from "./server-startup-matrix-migration-DR8lh8kT.js";
import { a as resolveNpmChannelTag, n as compareSemverStrings, p as normalizeUpdateChannel, t as checkUpdateStatus } from "./update-check-PKXwMGZy.js";
import { i as resolveGatewayStartupPluginIds, r as resolveConfiguredDeferredChannelPluginIds } from "./channel-plugin-ids-CvPuUsCE.js";
import "./setup.secret-input-DtZB3jrW.js";
import { t as runSetupWizard } from "./setup-Cx7PTqei.js";
import { a as resolveApnsAuthConfigFromEnv, c as shouldClearStoredApnsRegistration, d as parseMessageWithAttachments, f as formatForLog, g as setGatewayWsLogStyle, h as summarizeAgentEventForWsLog, l as resolveApnsRelayConfigFromEnv, m as shouldLogWs, n as loadApnsRegistration, o as sendApnsAlert, p as logWs, r as normalizeApnsEnvironment, s as sendApnsBackgroundWake, t as clearApnsRegistrationIfCurrent, u as normalizeRpcAttachmentsToChatAttachments } from "./push-apns-nhUon0wk.js";
import { _ as buildGogWatchStartArgs, g as buildGogWatchServeArgs, i as ensureTailscaleEndpoint, w as resolveGmailHookRuntimeConfig } from "./gmail-setup-utils-DdjUBu6M.js";
import { i as loadAgentIdentity, o as pruneAgentConfig, r as findAgentEntryIndex, t as applyAgentConfig } from "./agents.config-DWnSW7wb.js";
import { r as resolveRequestedBrowserProfile, t as isPersistentBrowserProfileMutation } from "./request-policy-Z4bqaQ0G.js";
import { n as isNodeCommandAllowed, r as resolveNodeCommandAllowlist } from "./node-command-policy-Ceicw2bH.js";
import { t as buildChannelAccountSnapshot } from "./status-MVAoMzDu.js";
import "./status-DjCnca8z.js";
import "./node-service-D4mvvQIK.js";
import "./channels-status-issues-DweHoyQa.js";
import "./channels-CRlN6PtH.js";
import "./status.update-k-L_a4xK.js";
import "./install-package-dir-ztoahuHZ.js";
import { i as updateSkillsFromClawHub, t as installSkillFromClawHub } from "./skills-clawhub-bSjmjMK7.js";
import "./skill-scanner-Cp-qCAUj.js";
import { t as installSkill } from "./skills-install-DPmJel4-.js";
import { t as resolveAllowedAgentIds } from "./hooks-policy-BaN_YwKW.js";
import { t as collectEnabledInsecureOrDangerousFlags } from "./dangerous-config-flags-DRxyeRNu.js";
import { t as loadWorkspaceHookEntries } from "./workspace-TnJmCeZO.js";
import { n as forceFreePortAndWait, r as waitForPortBindable } from "./ports-fW0NrR0l.js";
import { createRequire } from "node:module";
import { fileURLToPath, pathToFileURL } from "node:url";
import * as fsSync from "node:fs";
import fs from "node:fs";
import path from "node:path";
import { exec, spawn, spawnSync } from "node:child_process";
import os from "node:os";
import chalk from "chalk";
import { isDeepStrictEqual } from "node:util";
import fs$1 from "node:fs/promises";
import crypto, { createHash, randomBytes, randomUUID } from "node:crypto";
import { z } from "zod";
import net from "node:net";
import { CURRENT_SESSION_VERSION, SessionManager } from "@mariozechner/pi-coding-agent";
import { WebSocketServer } from "ws";
import { Buffer as Buffer$1 } from "node:buffer";
import { createServer as createServer$1 } from "node:https";
import { clearTimeout as clearTimeout$1, setTimeout as setTimeout$1 } from "node:timers";
import chokidar from "chokidar";
import { createServer as createServer$2 } from "node:http";
//#region src/commands/gateway-status/discovery.ts
function inferSshTargetFromRemoteUrl(rawUrl) {
if (typeof rawUrl !== "string") return null;
const trimmed = rawUrl.trim();
if (!trimmed) return null;
let host = null;
try {
host = new URL(trimmed).hostname || null;
} catch {
return null;
}
if (!host) return null;
const user = process.env.USER?.trim() || "";
return user ? `${user}@${host}` : host;
}
function buildSshTarget(input) {
const host = input.host?.trim() ?? "";
if (!host) return null;
const user = input.user?.trim() ?? "";
const base = user ? `${user}@${host}` : host;
const port = input.port ?? 22;
if (port && port !== 22) return `${base}:${port}`;
return base;
}
async function resolveSshTarget(params) {
const [{ resolveSshConfig }, { parseSshTarget }] = await Promise.all([params.loadSshConfigModule(), params.loadSshTunnelModule()]);
const parsed = parseSshTarget(params.rawTarget);
if (!parsed) return null;
const config = await resolveSshConfig(parsed, {
identity: params.identity ?? void 0,
timeoutMs: Math.min(800, params.overallTimeoutMs)
});
if (!config) return {
target: params.rawTarget,
identity: params.identity ?? void 0
};
const target = buildSshTarget({
user: config.user ?? parsed.user,
host: config.host ?? parsed.host,
port: config.port ?? parsed.port
});
if (!target) return {
target: params.rawTarget,
identity: params.identity ?? void 0
};
return {
target,
identity: params.identity ?? config.identityFiles.find((entry) => entry.trim().length > 0)?.trim() ?? void 0
};
}
function pickAutoSshTargetFromDiscovery(params) {
for (const beacon of params.discovery) {
const sshTarget = buildGatewayDiscoveryTarget(beacon, { sshUser: params.sshUser ?? void 0 }).sshTarget;
if (!sshTarget) continue;
if (params.parseSshTarget(sshTarget)) return sshTarget;
}
return null;
}
//#endregion
//#region src/commands/gateway-status/helpers.ts
const MISSING_SCOPE_PATTERN = /\bmissing scope:\s*[a-z0-9._-]+/i;
function parseIntOrNull(value) {
const s = typeof value === "string" ? value.trim() : typeof value === "number" || typeof value === "bigint" ? String(value) : "";
if (!s) return null;
const n = Number.parseInt(s, 10);
return Number.isFinite(n) ? n : null;
}
function parseTimeoutMs(raw, fallbackMs) {
return parseTimeoutMsWithFallback(raw, fallbackMs);
}
function normalizeWsUrl(value) {
const trimmed = value.trim();
if (!trimmed) return null;
if (!trimmed.startsWith("ws://") && !trimmed.startsWith("wss://")) return null;
return trimmed;
}
function resolveTargets(cfg, explicitUrl) {
const targets = [];
const add = (t) => {
if (!targets.some((x) => x.url === t.url)) targets.push(t);
};
const explicit = typeof explicitUrl === "string" ? normalizeWsUrl(explicitUrl) : null;
if (explicit) add({
id: "explicit",
kind: "explicit",
url: explicit,
active: true
});
const remoteUrl = typeof cfg.gateway?.remote?.url === "string" ? normalizeWsUrl(cfg.gateway.remote.url) : null;
if (remoteUrl) add({
id: "configRemote",
kind: "configRemote",
url: remoteUrl,
active: cfg.gateway?.mode === "remote"
});
add({
id: "localLoopback",
kind: "localLoopback",
url: `ws://127.0.0.1:${resolveGatewayPort(cfg)}`,
active: cfg.gateway?.mode !== "remote"
});
return targets;
}
function resolveProbeBudgetMs(overallMs, target) {
switch (target.kind) {
case "localLoopback": return target.active ? overallMs : Math.min(800, overallMs);
case "sshTunnel": return Math.min(2e3, overallMs);
default: return Math.min(1500, overallMs);
}
}
function sanitizeSshTarget(value) {
if (typeof value !== "string") return null;
const trimmed = value.trim();
if (!trimmed) return null;
return trimmed.replace(/^ssh\\s+/, "");
}
async function resolveAuthForTarget(cfg, target, overrides) {
const tokenOverride = overrides.token?.trim() ? overrides.token.trim() : void 0;
const passwordOverride = overrides.password?.trim() ? overrides.password.trim() : void 0;
if (tokenOverride || passwordOverride) return {
token: tokenOverride,
password: passwordOverride
};
const diagnostics = [];
const authMode = cfg.gateway?.auth?.mode;
const tokenOnly = authMode === "token";
const passwordOnly = authMode === "password";
const resolveToken = async (value, path) => {
const tokenResolution = await resolveConfiguredSecretInputString({
config: cfg,
env: process.env,
value,
path,
unresolvedReasonStyle: "detailed"
});
if (tokenResolution.unresolvedRefReason) diagnostics.push(tokenResolution.unresolvedRefReason);
return tokenResolution.value;
};
const resolvePassword = async (value, path) => {
const passwordResolution = await resolveConfiguredSecretInputString({
config: cfg,
env: process.env,
value,
path,
unresolvedReasonStyle: "detailed"
});
if (passwordResolution.unresolvedRefReason) diagnostics.push(passwordResolution.unresolvedRefReason);
return passwordResolution.value;
};
const withDiagnostics = (result) => diagnostics.length > 0 ? {
...result,
diagnostics
} : result;
if (target.kind === "configRemote" || target.kind === "sshTunnel") {
const remoteTokenValue = cfg.gateway?.remote?.token;
const remotePasswordValue = (cfg.gateway?.remote)?.password;
const token = await resolveToken(remoteTokenValue, "gateway.remote.token");
return withDiagnostics({
token,
password: token ? void 0 : await resolvePassword(remotePasswordValue, "gateway.remote.password")
});
}
if (authMode === "none" || authMode === "trusted-proxy") return {};
const envToken = readGatewayTokenEnv();
const envPassword = readGatewayPasswordEnv();
if (tokenOnly) {
const token = await resolveToken(cfg.gateway?.auth?.token, "gateway.auth.token");
if (token) return withDiagnostics({ token });
if (envToken) return { token: envToken };
return withDiagnostics({});
}
if (passwordOnly) {
const password = await resolvePassword(cfg.gateway?.auth?.password, "gateway.auth.password");
if (password) return withDiagnostics({ password });
if (envPassword) return { password: envPassword };
return withDiagnostics({});
}
const token = await resolveToken(cfg.gateway?.auth?.token, "gateway.auth.token");
if (token) return withDiagnostics({ token });
if (envToken) return { token: envToken };
if (envPassword) return withDiagnostics({ password: envPassword });
return withDiagnostics({
token,
password: await resolvePassword(cfg.gateway?.auth?.password, "gateway.auth.password")
});
}
function extractConfigSummary(snapshotUnknown) {
const snap = snapshotUnknown;
const path = typeof snap?.path === "string" ? snap.path : null;
const exists = Boolean(snap?.exists);
const valid = Boolean(snap?.valid);
const issuesRaw = Array.isArray(snap?.issues) ? snap.issues : [];
const legacyRaw = Array.isArray(snap?.legacyIssues) ? snap.legacyIssues : [];
const cfg = snap?.config ?? {};
const gateway = cfg.gateway ?? {};
const secretDefaults = (cfg.secrets ?? {}).defaults ?? void 0;
const wideArea = (cfg.discovery ?? {}).wideArea ?? {};
const remote = gateway.remote ?? {};
const auth = gateway.auth ?? {};
const controlUi = gateway.controlUi ?? {};
const tailscale = gateway.tailscale ?? {};
const authMode = typeof auth.mode === "string" ? auth.mode : null;
const authTokenConfigured = hasConfiguredSecretInput(auth.token, secretDefaults);
const authPasswordConfigured = hasConfiguredSecretInput(auth.password, secretDefaults);
const remoteUrl = typeof remote.url === "string" ? normalizeWsUrl(remote.url) : null;
const remoteTokenConfigured = hasConfiguredSecretInput(remote.token, secretDefaults);
const remotePasswordConfigured = hasConfiguredSecretInput(remote.password, secretDefaults);
const wideAreaEnabled = typeof wideArea.enabled === "boolean" ? wideArea.enabled : null;
return {
path,
exists,
valid,
issues: issuesRaw.filter((i) => Boolean(i && typeof i.path === "string" && typeof i.message === "string")).map((i) => ({
path: i.path,
message: i.message
})),
legacyIssues: legacyRaw.filter((i) => Boolean(i && typeof i.path === "string" && typeof i.message === "string")).map((i) => ({
path: i.path,
message: i.message
})),
gateway: {
mode: typeof gateway.mode === "string" ? gateway.mode : null,
bind: typeof gateway.bind === "string" ? gateway.bind : null,
port: parseIntOrNull(gateway.port),
controlUiEnabled: typeof controlUi.enabled === "boolean" ? controlUi.enabled : null,
controlUiBasePath: typeof controlUi.basePath === "string" ? controlUi.basePath : null,
authMode,
authTokenConfigured,
authPasswordConfigured,
remoteUrl,
remoteTokenConfigured,
remotePasswordConfigured,
tailscaleMode: typeof tailscale.mode === "string" ? tailscale.mode : null
},
discovery: { wideAreaEnabled }
};
}
function buildNetworkHints(cfg) {
const { tailnetIPv4 } = inspectBestEffortPrimaryTailnetIPv4();
const port = resolveGatewayPort(cfg);
return {
localLoopbackUrl: `ws://127.0.0.1:${port}`,
localTailnetUrl: tailnetIPv4 ? `ws://${tailnetIPv4}:${port}` : null,
tailnetIPv4: tailnetIPv4 ?? null
};
}
function renderTargetHeader(target, rich) {
const kindLabel = target.kind === "localLoopback" ? "Local loopback" : target.kind === "sshTunnel" ? "Remote over SSH" : target.kind === "configRemote" ? target.active ? "Remote (configured)" : "Remote (configured, inactive)" : "URL (explicit)";
return `${colorize(rich, theme.heading, kindLabel)} ${colorize(rich, theme.muted, target.url)}`;
}
function isScopeLimitedProbeFailure(probe) {
if (probe.ok || probe.connectLatencyMs == null) return false;
return MISSING_SCOPE_PATTERN.test(probe.error ?? "");
}
function isProbeReachable(probe) {
return probe.ok || isScopeLimitedProbeFailure(probe);
}
function renderProbeSummaryLine(probe, rich) {
if (probe.ok) {
const latency = typeof probe.connectLatencyMs === "number" ? `${probe.connectLatencyMs}ms` : "unknown";
return `${colorize(rich, theme.success, "Connect: ok")} (${latency}) · ${colorize(rich, theme.success, "RPC: ok")}`;
}
const detail = probe.error ? ` - ${probe.error}` : "";
if (probe.connectLatencyMs != null) {
const latency = typeof probe.connectLatencyMs === "number" ? `${probe.connectLatencyMs}ms` : "unknown";
const rpcStatus = isScopeLimitedProbeFailure(probe) ? colorize(rich, theme.warn, "RPC: limited") : colorize(rich, theme.error, "RPC: failed");
return `${colorize(rich, theme.success, "Connect: ok")} (${latency}) · ${rpcStatus}${detail}`;
}
return `${colorize(rich, theme.error, "Connect: failed")}${detail}`;
}
//#endregion
//#region src/commands/gateway-status/output.ts
function pickPrimaryProbedTarget(probed) {
const reachable = probed.filter((entry) => isProbeReachable(entry.probe));
return reachable.find((entry) => entry.target.kind === "explicit") ?? reachable.find((entry) => entry.target.kind === "sshTunnel") ?? reachable.find((entry) => entry.target.kind === "configRemote") ?? reachable.find((entry) => entry.target.kind === "localLoopback") ?? null;
}
function buildGatewayStatusWarnings(params) {
const reachable = params.probed.filter((entry) => isProbeReachable(entry.probe));
const degradedScopeLimited = params.probed.filter((entry) => isScopeLimitedProbeFailure(entry.probe));
const warnings = [];
if (params.sshTarget && !params.sshTunnelStarted) warnings.push({
code: "ssh_tunnel_failed",
message: params.sshTunnelError ? `SSH tunnel failed: ${String(params.sshTunnelError)}` : "SSH tunnel failed to start; falling back to direct probes."
});
if (reachable.length > 1) warnings.push({
code: "multiple_gateways",
message: "Unconventional setup: multiple reachable gateways detected. Usually one gateway per network is recommended unless you intentionally run isolated profiles, like a rescue bot (see docs: /gateway#multiple-gateways-same-host).",
targetIds: reachable.map((entry) => entry.target.id)
});
for (const result of params.probed) {
if (result.authDiagnostics.length === 0 || isProbeReachable(result.probe)) continue;
for (const diagnostic of result.authDiagnostics) warnings.push({
code: "auth_secretref_unresolved",
message: diagnostic,
targetIds: [result.target.id]
});
}
for (const result of degradedScopeLimited) warnings.push({
code: "probe_scope_limited",
message: "Probe diagnostics are limited by gateway scopes (missing operator.read). Connection succeeded, but status details may be incomplete. Hint: pair device identity or use credentials with operator.read.",
targetIds: [result.target.id]
});
return warnings;
}
function writeGatewayStatusJson(params) {
const reachable = params.probed.filter((entry) => isProbeReachable(entry.probe));
const degraded = params.probed.some((entry) => isScopeLimitedProbeFailure(entry.probe));
writeRuntimeJson(params.runtime, {
ok: reachable.length > 0,
degraded,
ts: Date.now(),
durationMs: Date.now() - params.startedAt,
timeoutMs: params.overallTimeoutMs,
primaryTargetId: params.primaryTargetId,
warnings: params.warnings,
network: params.network,
discovery: {
timeoutMs: params.discoveryTimeoutMs,
count: params.discovery.length,
beacons: params.discovery.map((beacon) => serializeGatewayDiscoveryBeacon(beacon))
},
targets: params.probed.map((entry) => ({
id: entry.target.id,
kind: entry.target.kind,
url: entry.target.url,
active: entry.target.active,
tunnel: entry.target.tunnel ?? null,
connect: {
ok: isProbeReachable(entry.probe),
rpcOk: entry.probe.ok,
scopeLimited: isScopeLimitedProbeFailure(entry.probe),
latencyMs: entry.probe.connectLatencyMs,
error: entry.probe.error,
close: entry.probe.close
},
self: entry.self,
config: entry.configSummary,
health: entry.probe.health,
summary: entry.probe.status,
presence: entry.probe.presence
}))
});
if (reachable.length === 0) params.runtime.exit(1);
}
function writeGatewayStatusText(params) {
const ok = params.probed.filter((entry) => isProbeReachable(entry.probe)).length > 0;
params.runtime.log(colorize(params.rich, theme.heading, "Gateway Status"));
params.runtime.log(ok ? `${colorize(params.rich, theme.success, "Reachable")}: yes` : `${colorize(params.rich, theme.error, "Reachable")}: no`);
params.runtime.log(colorize(params.rich, theme.muted, `Probe budget: ${params.overallTimeoutMs}ms`));
if (params.warnings.length > 0) {
params.runtime.log("");
params.runtime.log(colorize(params.rich, theme.warn, "Warning:"));
for (const warning of params.warnings) params.runtime.log(`- ${warning.message}`);
}
params.runtime.log("");
params.runtime.log(colorize(params.rich, theme.heading, "Discovery (this machine)"));
const discoveryDomains = params.wideAreaDomain ? `local. + ${params.wideAreaDomain}` : "local.";
params.runtime.log(params.discovery.length > 0 ? `Found ${params.discovery.length} gateway(s) via Bonjour (${discoveryDomains})` : `Found 0 gateways via Bonjour (${discoveryDomains})`);
if (params.discovery.length === 0) params.runtime.log(colorize(params.rich, theme.muted, "Tip: if the gateway is remote, mDNS won’t cross networks; use Wide-Area Bonjour (split DNS) or SSH tunnels."));
params.runtime.log("");
params.runtime.log(colorize(params.rich, theme.heading, "Targets"));
for (const result of params.probed) {
params.runtime.log(renderTargetHeader(result.target, params.rich));
params.runtime.log(` ${renderProbeSummaryLine(result.probe, params.rich)}`);
if (result.target.tunnel?.kind === "ssh") params.runtime.log(` ${colorize(params.rich, theme.muted, "ssh")}: ${colorize(params.rich, theme.command, result.target.tunnel.target)}`);
if (result.probe.ok && result.self) {
const host = result.self.host ?? "unknown";
const ip = result.self.ip ? ` (${result.self.ip})` : "";
const platform = result.self.platform ? ` · ${result.self.platform}` : "";
const version = result.self.version ? ` · app ${result.self.version}` : "";
params.runtime.log(` ${colorize(params.rich, theme.info, "Gateway")}: ${host}${ip}${platform}${version}`);
}
if (result.configSummary) {
const wideArea = result.configSummary.discovery.wideAreaEnabled === true ? "enabled" : result.configSummary.discovery.wideAreaEnabled === false ? "disabled" : "unknown";
params.runtime.log(` ${colorize(params.rich, theme.info, "Wide-area discovery")}: ${wideArea}`);
}
params.runtime.log("");
}
if (!ok) params.runtime.exit(1);
}
//#endregion
//#region src/commands/gateway-status/probe-run.ts
async function runGatewayStatusProbePass(params) {
const discoveryPromise = discoverGatewayBeacons({
timeoutMs: params.discoveryTimeoutMs,
wideAreaDomain: params.wideAreaDomain
});
let sshTarget = params.sshTarget;
let sshTunnelError = null;
let sshTunnelStarted = false;
const tryStartTunnel = async () => {
if (!sshTarget) return null;
try {
const { startSshPortForward } = await params.loadSshTunnelModule();
const tunnel = await startSshPortForward({
target: sshTarget,
identity: params.sshIdentity ?? void 0,
localPortPreferred: params.remotePort,
remotePort: params.remotePort,
timeoutMs: Math.min(1500, params.overallTimeoutMs)
});
sshTunnelStarted = true;
return tunnel;
} catch (err) {
sshTunnelError = err instanceof Error ? err.message : String(err);
return null;
}
};
const discoveryTask = discoveryPromise.catch(() => []);
const tunnelTask = sshTarget ? tryStartTunnel() : Promise.resolve(null);
const [discovery, tunnelFirst] = await Promise.all([discoveryTask, tunnelTask]);
if (!sshTarget && params.opts.sshAuto) {
const { parseSshTarget } = await params.loadSshTunnelModule();
sshTarget = pickAutoSshTargetFromDiscovery({
discovery,
parseSshTarget,
sshUser: process.env.USER?.trim() || ""
});
}
const tunnel = tunnelFirst || (sshTarget && !sshTunnelStarted && !sshTunnelError ? await tryStartTunnel() : null);
const tunnelTarget = tunnel ? {
id: "sshTunnel",
kind: "sshTunnel",
url: `ws://127.0.0.1:${tunnel.localPort}`,
active: true,
tunnel: {
kind: "ssh",
target: sshTarget ?? "",
localPort: tunnel.localPort,
remotePort: params.remotePort,
pid: tunnel.pid
}
} : null;
const targets = tunnelTarget ? [tunnelTarget, ...params.baseTargets.filter((target) => target.url !== tunnelTarget.url)] : params.baseTargets;
try {
return {
discovery,
probed: await Promise.all(targets.map(async (target) => {
const authResolution = await resolveAuthForTarget(params.cfg, target, {
token: typeof params.opts.token === "string" ? params.opts.token : void 0,
password: typeof params.opts.password === "string" ? params.opts.password : void 0
});
const probe = await probeGateway({
url: target.url,
auth: {
token: authResolution.token,
password: authResolution.password
},
timeoutMs: resolveProbeBudgetMs(params.overallTimeoutMs, target)
});
return {
target,
probe,
configSummary: probe.configSnapshot ? extractConfigSummary(probe.configSnapshot) : null,
self: pickGatewaySelfPresence(probe.presence),
authDiagnostics: authResolution.diagnostics ?? []
};
})),
sshTarget,
sshTunnelStarted,
sshTunnelError
};
} finally {
if (tunnel) try {
await tunnel.stop();
} catch {}
}
}
//#endregion
//#region src/commands/gateway-status.ts
let sshConfigModulePromise;
let sshTunnelModulePromise;
function loadSshConfigModule() {
sshConfigModulePromise ??= import("./ssh-config-DtzxUdry.js");
return sshConfigModulePromise;
}
function loadSshTunnelModule() {
sshTunnelModulePromise ??= import("./ssh-tunnel-BIzeMzLQ.js");
return sshTunnelModulePromise;
}
async function gatewayStatusCommand(opts, runtime) {
const startedAt = Date.now();
const cfg = await readBestEffortConfig();
const rich = isRich() && opts.json !== true;
const overallTimeoutMs = parseTimeoutMs(opts.timeout, 3e3);
const wideAreaDomain = resolveWideAreaDiscoveryDomain({ configDomain: cfg.discovery?.wideArea?.domain });
const baseTargets = resolveTargets(cfg, opts.url);
const network = buildNetworkHints(cfg);
const remotePort = resolveGatewayPort(cfg);
const discoveryTimeoutMs = Math.min(1200, overallTimeoutMs);
let sshTarget = sanitizeSshTarget(opts.ssh) ?? sanitizeSshTarget(cfg.gateway?.remote?.sshTarget);
let sshIdentity = sanitizeSshTarget(opts.sshIdentity) ?? sanitizeSshTarget(cfg.gateway?.remote?.sshIdentity);
if (!sshTarget) sshTarget = inferSshTargetFromRemoteUrl(cfg.gateway?.remote?.url);
if (sshTarget) {
const resolved = await resolveSshTarget({
rawTarget: sshTarget,
identity: sshIdentity,
overallTimeoutMs,
loadSshConfigModule,
loadSshTunnelModule
});
if (resolved) {
sshTarget = resolved.target;
if (!sshIdentity && resolved.identity) sshIdentity = resolved.identity;
}
}
const probePass = await withProgress({
label: "Inspecting gateways…",
indeterminate: true,
enabled: opts.json !== true
}, async () => await runGatewayStatusProbePass({
cfg,
opts,
overallTimeoutMs,
discoveryTimeoutMs,
wideAreaDomain,
baseTargets,
remotePort,
sshTarget,
sshIdentity,
loadSshTunnelModule
}));
const warnings = buildGatewayStatusWarnings({
probed: probePass.probed,
sshTarget: probePass.sshTarget,
sshTunnelStarted: probePass.sshTunnelStarted,
sshTunnelError: probePass.sshTunnelError
});
const primary = pickPrimaryProbedTarget(probePass.probed);
if (opts.json) {
writeGatewayStatusJson({
runtime,
startedAt,
overallTimeoutMs,
discoveryTimeoutMs,
network,
discovery: probePass.discovery,
probed: probePass.probed,
warnings,
primaryTargetId: primary?.target.id ?? null
});
return;
}
writeGatewayStatusText({
runtime,
rich,
overallTimeoutMs,
wideAreaDomain,
discovery: probePass.discovery,
probed: probePass.probed,
warnings
});
}
//#endregion
//#region src/cli/gateway-cli/call.ts
const gatewayCallOpts = (cmd) => cmd.option("--url <url>", "Gateway WebSocket URL (defaults to gateway.remote.url when configured)").option("--token <token>", "Gateway token (if required)").option("--password <password>", "Gateway password (password auth)").option("--timeout <ms>", "Timeout in ms", "10000").option("--expect-final", "Wait for final response (agent)", false).option("--json", "Output JSON", false);
const callGatewayCli = async (method, opts, params) => withProgress({
label: `Gateway ${method}`,
indeterminate: true,
enabled: opts.json !== true
}, async () => await callGateway({
config: opts.config,
url: opts.url,
token: opts.token,
password: opts.password,
method,
params,
expectFinal: Boolean(opts.expectFinal),
timeoutMs: Number(opts.timeout ?? 1e4),
clientName: GATEWAY_CLIENT_NAMES.CLI,
mode: GATEWAY_CLIENT_MODES.CLI
}));
//#endregion
//#region src/cli/gateway-cli/discover.ts
function parseDiscoverTimeoutMs(raw, fallbackMs) {
return parseTimeoutMsWithFallback(raw, fallbackMs, { invalidType: "error" });
}
function pickBeaconHost(beacon) {
return buildGatewayDiscoveryTarget(beacon).endpoint?.host ?? null;
}
function pickGatewayPort(beacon) {
return buildGatewayDiscoveryTarget(beacon).endpoint?.port ?? null;
}
function dedupeBeacons(beacons) {
const out = [];
const seen = /* @__PURE__ */ new Set();
for (const b of beacons) {
const host = pickBeaconHost(b) ?? "";
const key = [
b.domain ?? "",
b.instanceName ?? "",
b.displayName ?? "",
host,
String(b.port ?? ""),
String(b.gatewayPort ?? "")
].join("|");
if (seen.has(key)) continue;
seen.add(key);
out.push(b);
}
return out;
}
function renderBeaconLines(beacon, rich) {
const target = buildGatewayDiscoveryTarget(beacon);
const lines = [`- ${colorize(rich, theme.accentBright, target.title)} ${colorize(rich, theme.muted, target.domain)}`];
if (beacon.tailnetDns) lines.push(` ${colorize(rich, theme.info, "tailnet")}: ${beacon.tailnetDns}`);
if (beacon.lanHost) lines.push(` ${colorize(rich, theme.info, "lan")}: ${beacon.lanHost}`);
if (beacon.host) lines.push(` ${colorize(rich, theme.info, "host")}: ${beacon.host}`);
if (target.wsUrl) lines.push(` ${colorize(rich, theme.muted, "ws")}: ${colorize(rich, theme.command, target.wsUrl)}`);
if (beacon.role) lines.push(` ${colorize(rich, theme.muted, "role")}: ${beacon.role}`);
if (beacon.transport) lines.push(` ${colorize(rich, theme.muted, "transport")}: ${beacon.transport}`);
if (beacon.gatewayTls) {
const fingerprint = beacon.gatewayTlsFingerprintSha256 ? `sha256 ${beacon.gatewayTlsFingerprintSha256}` : "enabled";
lines.push(` ${colorize(rich, theme.muted, "tls")}: ${fingerprint}`);
}
if (target.endpoint && target.sshPort) {
const ssh = `ssh -N -L 18789:127.0.0.1:18789 <user>@${target.endpoint.host} -p ${target.sshPort}`;
lines.push(` ${colorize(rich, theme.muted, "ssh")}: ${colorize(rich, theme.command, ssh)}`);
}
return lines;
}
//#endregion
//#region src/gateway/server/close-reason.ts
const CLOSE_REASON_MAX_BYTES = 120;
function truncateCloseReason(reason, maxBytes = CLOSE_REASON_MAX_BYTES) {
if (!reason) return "invalid handshake";
const buf = Buffer$1.from(reason);
if (buf.length <= maxBytes) return reason;
return buf.subarray(0, maxBytes).toString();
}
//#endregion
//#region src/infra/exec-approval-forwarder.ts
const log$6 = createSubsystemLogger("gateway/exec-approvals");
const DEFAULT_MODE = "session";
function normalizeMode(mode) {
return mode ?? DEFAULT_MODE;
}
function matchSessionFilter(sessionKey, patterns) {
return patterns.some((pattern) => {
if (sessionKey.includes(pattern)) return true;
const compiled = compileConfigRegex(pattern);
return compiled?.regex ? testRegexWithBoundedInput(compiled.regex, sessionKey) : false;
});
}
function shouldForward(params) {
const config = params.config;
if (!config?.enabled) return false;
if (config.agentFilter?.length) {
const agentId = params.request.request.agentId ?? parseAgentSessionKey(params.request.request.sessionKey)?.agentId;
if (!agentId) return false;
if (!config.agentFilter.includes(agentId)) return false;
}
if (config.sessionFilter?.length) {
const sessionKey = params.request.request.sessionKey;
if (!sessionKey) return false;
if (!matchSessionFilter(sessionKey, config.sessionFilter)) return false;
}
return true;
}
function buildTargetKey(target) {
const channel = normalizeMessageChannel(target.channel) ?? target.channel;
const accountId = target.accountId ?? "";
const threadId = target.threadId ?? "";
return [
channel,
target.to,
accountId,
threadId
].join(":");
}
function shouldSkipForwardingFallback(params) {
const channel = normalizeMessageChannel(params.target.channel) ?? params.target.channel;
if (!channel) return false;
return (getChannelPlugin(channel)?.execApprovals)?.shouldSuppressForwardingFallback?.({
cfg: params.cfg,
target: params.target,
request: params.request
}) ?? false;
}
function formatApprovalCommand(command) {
if (!command.includes("\n") && !command.includes("`")) return {
inline: true,
text: `\`${command}\``
};
let fence = "```";
while (command.includes(fence)) fence += "`";
return {
inline: false,
text: `${fence}\n${command}\n${fence}`
};
}
function buildRequestMessage(request, nowMs) {
const lines = ["🔒 Exec approval required", `ID: ${request.id}`];
const command = formatApprovalCommand(resolveExecApprovalCommandDisplay(request.request).commandText);
if (command.inline) lines.push(`Command: ${command.text}`);
else {
lines.push("Command:");
lines.push(command.text);
}
if (request.request.cwd) lines.push(`CWD: ${request.request.cwd}`);
if (request.request.nodeId) lines.push(`Node: ${request.request.nodeId}`);
if (Array.isArray(request.request.envKeys) && request.request.envKeys.length > 0) lines.push(`Env overrides: ${request.request.envKeys.join(", ")}`);
if (request.request.host) lines.push(`Host: ${request.request.host}`);
if (request.request.agentId) lines.push(`Agent: ${request.request.agentId}`);
if (request.request.security) lines.push(`Security: ${request.request.security}`);
if (request.request.ask) lines.push(`Ask: ${request.request.ask}`);
const expiresIn = Math.max(0, Math.round((request.expiresAtMs - nowMs) / 1e3));
lines.push(`Expires in: ${expiresIn}s`);
lines.push("Mode: foreground (interactive approvals available in this chat).");
lines.push("Background mode note: non-interactive runs cannot wait for chat approvals; use pre-approved policy (allow-always or ask=off).");
lines.push("Reply with: /approve <id> allow-once|allow-always|deny");
return lines.join("\n");
}
function decisionLabel(decision) {
if (decision === "allow-once") return "allowed once";
if (decision === "allow-always") return "allowed always";
return "denied";
}
function buildResolvedMessage(resolved) {
return `${`✅ Exec approval ${decisionLabel(resolved.decision)}.`}${resolved.resolvedBy ? ` Resolved by ${resolved.resolvedBy}.` : ""} ID: ${resolved.id}`;
}
function buildExpiredMessage(request) {
return `⏱️ Exec approval expired. ID: ${request.id}`;
}
function normalizeTurnSourceChannel(value) {
const normalized = value ? normalizeMessageChannel(value) : void 0;
return normalized && isDeliverableMessageChannel(normalized) ? normalized : void 0;
}
function defaultResolveSessionTarget(params) {
const resolvedTarget = resolveExecApprovalSessionTarget({
cfg: params.cfg,
request: params.request,
turnSourceChannel: normalizeTurnSourceChannel(params.request.request.turnSourceChannel),
turnSourceTo: params.request.request.turnSourceTo?.trim() || void 0,
turnSourceAccountId: params.request.request.turnSourceAccountId?.trim() || void 0,
turnSourceThreadId: params.request.request.turnSourceThreadId ?? void 0
});
if (!resolvedTarget?.channel || !resolvedTarget.to) return null;
const channel = resolvedTarget.channel;
if (!isDeliverableMessageChannel(channel)) return null;
return {
channel,
to: resolvedTarget.to,
accountId: resolvedTarget.accountId,
threadId: resolvedTarget.threadId
};
}
async function deliverToTargets(params) {
const deliveries = params.targets.map(async (target) => {
if (params.shouldSend && !params.shouldSend()) return;
const channel = normalizeMessageChannel(target.channel) ?? target.channel;
if (!isDeliverableMessageChannel(channel)) return;
try {
const payload = params.buildPayload(target);
await params.beforeDeliver?.(target, payload);
await params.deliver({
cfg: params.cfg,
channel,
to: target.to,
accountId: target.accountId,
threadId: target.threadId,
payloads: [payload]
});
} catch (err) {
log$6.error(`exec approvals: failed to deliver to ${channel}:${target.to}: ${String(err)}`);
}
});
await Promise.allSettled(deliveries);
}
function buildRequestPayloadForTarget(cfg, request, nowMsValue, target) {
const channel = normalizeMessageChannel(target.channel) ?? target.channel;
const pluginPayload = channel ? getChannelPlugin(channel)?.execApprovals?.buildPendingPayload?.({
cfg,
request,
target,
nowMs: nowMsValue
}) : null;
if (pluginPayload) return pluginPayload;
return { text: buildRequestMessage(request, nowMsValue) };
}
function buildResolvedPayloadForTarget(cfg, resolved, target) {
const channel = normalizeMessageChannel(target.channel) ?? target.channel;
const pluginPayload = channel ? getChannelPlugin(channel)?.execApprovals?.buildResolvedPayload?.({
cfg,
resolved,
target
}) : null;
if (pluginPayload) return pluginPayload;
return { text: buildResolvedMessage(resolved) };
}
function resolveForwardTargets(params) {
const mode = normalizeMode(params.config?.mode);
const targets = [];
const seen = /* @__PURE__ */ new Set();
if (mode === "session" || mode === "both") {
const sessionTarget = params.resolveSessionTarget({
cfg: params.cfg,
request: params.request
});
if (sessionTarget) {
const key = buildTargetKey(sessionTarget);
if (!seen.has(key)) {
seen.add(key);
targets.push({
...sessionTarget,
source: "session"
});
}
}
}
if (mode === "targets" || mode === "both") {
const explicitTargets = params.config?.targets ?? [];
for (const target of explicitTargets) {
const key = buildTargetKey(target);
if (seen.has(key)) continue;
seen.add(key);
targets.push({
...target,
source: "target"
});
}
}
return targets;
}
function createExecApprovalForwarder(deps = {}) {
const getConfig = deps.getConfig ?? loadConfig;
const deliver = deps.deliver ?? deliverOutboundPayloads;
const nowMs = deps.nowMs ?? Date.now;
const resolveSessionTarget = deps.resolveSessionTarget ?? defaultResolveSessionTarget;
const pending = /* @__PURE__ */ new Map();
const handleRequested = async (request) => {
const cfg = getConfig();
const config = cfg.approvals?.exec;
const filteredTargets = [...shouldForward({
config,
request
}) ? resolveForwardTargets({
cfg,
config,
request,
resolveSessionTarget
}) : []].filter((target) => !shouldSkipForwardingFallback({
target,
cfg,
request
}));
if (filteredTargets.length === 0) return false;
const expiresInMs = Math.max(0, request.expiresAtMs - nowMs());
const timeoutId = setTimeout(() => {
(async () => {
const entry = pending.get(request.id);
if (!entry) return;
pending.delete(request.id);
const expiredText = buildExpiredMessage(request);
await deliverToTargets({
cfg,
targets: entry.targets,
buildPayload: () => ({ text: expiredText }),
deliver
});
})();
}, expiresInMs);
timeoutId.unref?.();
const pendingEntry = {
request,
targets: filteredTargets,
timeoutId
};
pending.set(request.id, pendingEntry);
if (pending.get(request.id) !== pendingEntry) return false;
deliverToTargets({
cfg,
targets: filteredTargets,
buildPayload: (target) => buildRequestPayloadForTarget(cfg, request, nowMs(), target),
beforeDeliver: async (target, payload) => {
const channel = normalizeMessageChannel(target.channel) ?? target.channel;
if (!channel) return;
await getChannelPlugin(channel)?.execApprovals?.beforeDeliverPending?.({
cfg,
target,
payload
});
},
deliver,
shouldSend: () => pending.get(request.id) === pendingEntry
}).catch((err) => {
log$6.error(`exec approvals: failed to deliver request ${request.id}: ${String(err)}`);
});
return true;
};
const handleResolved = async (resolved) => {
const entry = pending.get(resolved.id);
if (entry) {
if (entry.timeoutId) clearTimeout(entry.timeoutId);
pending.delete(resolved.id);
}
const cfg = getConfig();
let targets = entry?.targets;
if (!targets && resolved.request) {
const request = {
id: resolved.id,
request: resolved.request,
createdAtMs: resolved.ts,
expiresAtMs: resolved.ts
};
const config = cfg.approvals?.exec;
targets = [...shouldForward({
config,
request
}) ? resolveForwardTargets({
cfg,
config,
request,
resolveSessionTarget
}) : []].filter((target) => !shouldSkipForwardingFallback({
target,
cfg,
request
}));
}
if (!targets || targets.length === 0) return;
await deliverToTargets({
cfg,
targets,
buildPayload: (target) => buildResolvedPayloadForTarget(cfg, resolved, target),
deliver
});
};
const stop = () => {
for (const entry of pending.values()) if (entry.timeoutId) clearTimeout(entry.timeoutId);
pending.clear();
};
return {
handleRequested,
handleResolved,
stop
};
}
//#endregion
//#region src/cron/isolated-agent/session.ts
function resolveCronSession(params) {
const sessionCfg = params.cfg.session;
const storePath = resolveStorePath(sessionCfg?.store, { agentId: params.agentId });
const store = loadSessionStore(storePath);
const entry = store[params.sessionKey];
let sessionId;
let isNewSession;
let systemSent;
if (!params.forceNew && entry?.sessionId) {
const resetPolicy = resolveSessionResetPolicy({
sessionCfg,
resetType: "direct"
});
if (evaluateSessionFreshness({
updatedAt: entry.updatedAt,
now: params.nowMs,
policy: resetPolicy
}).fresh) {
sessionId = entry.sessionId;
isNewSession = false;
systemSent = entry.systemSent ?? false;
} else {
sessionId = crypto.randomUUID();
isNewSession = true;
systemSent = false;
}
} else {
sessionId = crypto.randomUUID();
isNewSession = true;
systemSent = false;
}
clearBootstrapSnapshotOnSessionRollover({
sessionKey: params.sessionKey,
previousSessionId: isNewSession ? entry?.sessionId : void 0
});
return {
storePath,
store,
sessionEntry: {
...entry,
sessionId,
updatedAt: params.nowMs,
systemSent,
...isNewSession && {
lastChannel: void 0,
lastTo: void 0,
lastAccountId: void 0,
lastThreadId: void 0,
deliveryContext: void 0
}
},
systemSent,
isNewSession
};
}
//#endregion
//#region src/infra/heartbeat-active-hours.ts
const ACTIVE_HOURS_TIME_PATTERN = /^(?:([01]\d|2[0-3]):([0-5]\d)|24:00)$/;
function resolveActiveHoursTimezone(cfg, raw) {
const trimmed = raw?.trim();
if (!trimmed || trimmed === "user") return resolveUserTimezone(cfg.agents?.defaults?.userTimezone);
if (trimmed === "local") return Intl.DateTimeFormat().resolvedOptions().timeZone?.trim() || "UTC";
try {
new Intl.DateTimeFormat("en-US", { timeZone: trimmed }).format(/* @__PURE__ */ new Date());
return trimmed;
} catch {
return resolveUserTimezone(cfg.agents?.defaults?.userTimezone);
}
}
function parseActiveHoursTime(opts, raw) {
if (!raw || !ACTIVE_HOURS_TIME_PATTERN.test(raw)) return null;
const [hourStr, minuteStr] = raw.split(":");
const hour = Number(hourStr);
const minute = Number(minuteStr);
if (!Number.isFinite(hour) || !Number.isFinite(minute)) return null;
if (hour === 24) {
if (!opts.allow24 || minute !== 0) return null;
return 1440;
}
return hour * 60 + minute;
}
function resolveMinutesInTimeZone(nowMs, timeZone) {
try {
const parts = new Intl.DateTimeFormat("en-US", {
timeZone,
hour: "2-digit",
minute: "2-digit",
hourCycle: "h23"
}).formatToParts(new Date(nowMs));
const map = {};
for (const part of parts) if (part.type !== "literal") map[part.type] = part.value;
const hour = Number(map.hour);
const minute = Number(map.minute);
if (!Number.isFinite(hour) || !Number.isFinite(minute)) return null;
return hour * 60 + minute;
} catch {
return null;
}
}
function isWithinActiveHours(cfg, heartbeat, nowMs) {
const active = heartbeat?.activeHours;
if (!active) return true;
const startMin = parseActiveHoursTime({ allow24: false }, active.start);
const endMin = parseActiveHoursTime({ allow24: true }, active.end);
if (startMin === null || endMin === null) return true;
if (startMin === endMin) return false;
const timeZone = resolveActiveHoursTimezone(cfg, active.timezone);
const currentMin = resolveMinutesInTimeZone(nowMs ?? Date.now(), timeZone);
if (currentMin === null) return true;
if (endMin > startMin) return currentMin >= startMin && currentMin < endMin;
return currentMin >= startMin || currentMin < endMin;
}
//#endregion
//#region src/infra/heartbeat-events-filter.ts
function buildCronEventPrompt(pendingEvents, opts) {
const deliverToUser = opts?.deliverToUser ?? true;
const eventText = pendingEvents.join("\n").trim();
if (!eventText) {
if (!deliverToUser) return "A scheduled cron event was triggered, but no event content was found. Handle this internally and reply HEARTBEAT_OK when nothing needs user-facing follow-up.";
return "A scheduled cron event was triggered, but no event content was found. Reply HEARTBEAT_OK.";
}
if (!deliverToUser) return "A scheduled reminder has been triggered. The reminder content is:\n\n" + eventText + "\n\nHandle this reminder internally. Do not relay it to the user unless explicitly requested.";
return "A scheduled reminder has been triggered. The reminder content is:\n\n" + eventText + "\n\nPlease relay this reminder to the user in a helpful and friendly way.";
}
function buildExecEventPrompt(opts) {
if (!(opts?.deliverToUser ?? true)) return "An async command you ran earlier has completed. The result is shown in the system messages above. Handle the result internally. Do not relay it to the user unless explicitly requested.";
return "An async command you ran earlier has completed. The result is shown in the system messages above. Please relay the command output to the user in a helpful way. If the command succeeded, share the relevant output. If it failed, explain what went wrong.";
}
const HEARTBEAT_OK_PREFIX = HEARTBEAT_TOKEN.toLowerCase();
function isHeartbeatAckEvent(evt) {
const trimmed = evt.trim();
if (!trimmed) return false;
const lower = trimmed.toLowerCase();
if (!lower.startsWith(HEARTBEAT_OK_PREFIX)) return false;
const suffix = lower.slice(HEARTBEAT_OK_PREFIX.length);
if (suffix.length === 0) return true;
return !/[a-z0-9_]/.test(suffix[0]);
}
function isHeartbeatNoiseEvent(evt) {
const lower = evt.trim().toLowerCase();
if (!lower) return false;
return isHeartbeatAckEvent(lower) || lower.includes("heartbeat poll") || lower.includes("heartbeat wake");
}
function isExecCompletionEvent(evt) {
return evt.toLowerCase().includes("exec finished");
}
function isCronSystemEvent(evt) {
if (!evt.trim()) return false;
return !isHeartbeatNoiseEvent(evt) && !isExecCompletionEvent(evt);
}
//#endregion
//#region src/infra/heartbeat-runner.ts
const log$5 = createSubsystemLogger("gateway/heartbeat");
function hasExplicitHeartbeatAgents(cfg) {
return (cfg.agents?.list ?? []).some((entry) => Boolean(entry?.heartbeat));
}
function resolveHeartbeatConfig(cfg, agentId) {
const defaults = cfg.agents?.defaults?.heartbeat;
if (!agentId) return defaults;
const overrides = resolveAgentConfig(cfg, agentId)?.heartbeat;
if (!defaults && !overrides) return overrides;
return {
...defaults,
...overrides
};
}
function resolveHeartbeatAgents(cfg) {
const list = cfg.agents?.list ?? [];
if (hasExplicitHeartbeatAgents(cfg)) return list.filter((entry) => entry?.heartbeat).map((entry) => {
const id = normalizeAgentId(entry.id);
return {
agentId: id,
heartbeat: resolveHeartbeatConfig(cfg, id)
};
}).filter((entry) => entry.agentId);
const fallbackId = resolveDefaultAgentId(cfg);
return [{
agentId: fallbackId,
heartbeat: resolveHeartbeatConfig(cfg, fallbackId)
}];
}
function resolveHeartbeatPrompt(cfg, heartbeat) {
return resolveHeartbeatPrompt$1(heartbeat?.prompt ?? cfg.agents?.defaults?.heartbeat?.prompt);
}
function resolveHeartbeatAckMaxChars$2(cfg, heartbeat) {
return Math.max(0, heartbeat?.ackMaxChars ?? cfg.agents?.defaults?.heartbeat?.ackMaxChars ?? 300);
}
function resolveHeartbeatSession(cfg, agentId, heartbeat, forcedSessionKey) {
const sessionCfg = cfg.session;
const scope = sessionCfg?.scope ?? "per-sender";
const resolvedAgentId = normalizeAgentId(agentId ?? resolveDefaultAgentId(cfg));
const mainSessionKey = scope === "global" ? "global" : resolveAgentMainSessionKey({
cfg,
agentId: resolvedAgentId
});
const storeAgentId = scope === "global" ? resolveDefaultAgentId(cfg) : resolvedAgentId;
const storePath = resolveStorePath(sessionCfg?.store, { agentId: storeAgentId });
const store = loadSessionStore(storePath);
const mainEntry = store[mainSessionKey];
if (scope === "global") return {
sessionKey: mainSessionKey,
storePath,
store,
entry: mainEntry
};
const forced = forcedSessionKey?.trim();
if (forced) {
const forcedCanonical = canonicalizeMainSessionAlias({
cfg,
agentId: resolvedAgentId,
sessionKey: toAgentStoreSessionKey({
agentId: resolvedAgentId,
requestKey: forced,
mainKey: cfg.session?.mainKey
})
});
if (forcedCanonical !== "global") {
if (resolveAgentIdFromSessionKey(forcedCanonical) === normalizeAgentId(resolvedAgentId)) return {
sessionKey: forcedCanonical,
storePath,
store,
entry: store[forcedCanonical]
};
}
}
const trimmed = heartbeat?.session?.trim() ?? "";
if (!trimmed) return {
sessionKey: mainSessionKey,
storePath,
store,
entry: mainEntry
};
const normalized = trimmed.toLowerCase();
if (normalized === "main" || normalized === "global") return {
sessionKey: mainSessionKey,
storePath,
store,
entry: mainEntry
};
const canonical = canonicalizeMainSessionAlias({
cfg,
agentId: resolvedAgentId,
sessionKey: toAgentStoreSessionKey({
agentId: resolvedAgentId,
requestKey: trimmed,
mainKey: cfg.session?.mainKey
})
});
if (canonical !== "global") {
if (resolveAgentIdFromSessionKey(canonical) === normalizeAgentId(resolvedAgentId)) return {
sessionKey: canonical,
storePath,
store,
entry: store[canonical]
};
}
return {
sessionKey: mainSessionKey,
storePath,
store,
entry: mainEntry
};
}
function resolveHeartbeatReasoningPayloads(replyResult) {
return (Array.isArray(replyResult) ? replyResult : replyResult ? [replyResult] : []).filter((payload) => {
return (typeof payload.text === "string" ? payload.text : "").trimStart().startsWith("Reasoning:");
});
}
async function restoreHeartbeatUpdatedAt(params) {
const { storePath, sessionKey, updatedAt } = params;
if (typeof updatedAt !== "number") return;
const entry = loadSessionStore(storePath)[sessionKey];
if (!entry) return;
const nextUpdatedAt = Math.max(entry.updatedAt ?? 0, updatedAt);
if (entry.updatedAt === nextUpdatedAt) return;
await updateSessionStore(storePath, (nextStore) => {
const nextEntry = nextStore[sessionKey] ?? entry;
if (!nextEntry) return;
const resolvedUpdatedAt = Math.max(nextEntry.updatedAt ?? 0, updatedAt);
if (nextEntry.updatedAt === resolvedUpdatedAt) return;
nextStore[sessionKey] = {
...nextEntry,
updatedAt: resolvedUpdatedAt
};
});
}
/**
* Prune heartbeat transcript entries by truncating the file back to a previous size.
* This removes the user+assistant turns that were written during a HEARTBEAT_OK run,
* preventing context pollution from zero-information exchanges.
*/
async function pruneHeartbeatTranscript(params) {
const { transcriptPath, preHeartbeatSize } = params;
if (!transcriptPath || typeof preHeartbeatSize !== "number" || preHeartbeatSize < 0) return;
try {
if ((await fs$1.stat(transcriptPath)).size > preHeartbeatSize) await fs$1.truncate(transcriptPath, preHeartbeatSize);
} catch {}
}
/**
* Get the transcript file path and its current size before a heartbeat run.
* Returns undefined values if the session or transcript doesn't exist yet.
*/
async function captureTranscriptState(params) {
const { storePath, sessionKey, agentId } = params;
try {
const entry = loadSessionStore(storePath)[sessionKey];
if (!entry?.sessionId) return {};
const transcriptPath = resolveSessionFilePath(entry.sessionId, entry, {
agentId,
sessionsDir: path.dirname(storePath)
});
return {
transcriptPath,
preHeartbeatSize: (await fs$1.stat(transcriptPath)).size
};
} catch {
return {};
}
}
function stripLeadingHeartbeatResponsePrefix(text, responsePrefix) {
const normalizedPrefix = responsePrefix?.trim();
if (!normalizedPrefix) return text;
const prefixPattern = new RegExp(`^${escapeRegExp(normalizedPrefix)}(?=$|\\s|[\\p{P}\\p{S}])\\s*`, "iu");
return text.replace(prefixPattern, "");
}
function normalizeHeartbeatReply(payload, responsePrefix, ackMaxChars) {
const stripped = stripHeartbeatToken(stripLeadingHeartbeatResponsePrefix(typeof payload.text === "string" ? payload.text : "", responsePrefix), {
mode: "heartbeat",
maxAckChars: ackMaxChars
});
const hasMedia = resolveSendableOutboundReplyParts(payload).hasMedia;
if (stripped.shouldSkip && !hasMedia) return {
shouldSkip: true,
text: "",
hasMedia
};
let finalText = stripped.text;
if (responsePrefix && finalText && !finalText.startsWith(responsePrefix)) finalText = `${responsePrefix} ${finalText}`;
return {
shouldSkip: false,
text: finalText,
hasMedia
};
}
function resolveHeartbeatReasonFlags(reason) {
const reasonKind = resolveHeartbeatReasonKind(reason);
return {
isExecEventReason: reasonKind === "exec-event",
isCronEventReason: reasonKind === "cron",
isWakeReason: reasonKind === "wake" || reasonKind === "hook"
};
}
async function resolveHeartbeatPreflight(params) {
const reasonFlags = resolveHeartbeatReasonFlags(params.reason);
const session = resolveHeartbeatSession(params.cfg, params.agentId, params.heartbeat, params.forcedSessionKey);
const pendingEventEntries = peekSystemEventEntries(session.sessionKey);
const hasTaggedCronEvents = pendingEventEntries.some((event) => event.contextKey?.startsWith("cron:"));
const shouldInspectPendingEvents = reasonFlags.isExecEventReason || reasonFlags.isCronEventReason || hasTaggedCronEvents;
const shouldBypassFileGates = reasonFlags.isExecEventReason || reasonFlags.isCronEventReason || reasonFlags.isWakeReason || hasTaggedCronEvents;
const basePreflight = {
...reasonFlags,
session,
pendingEventEntries,
hasTaggedCronEvents,
shouldInspectPendingEvents
};
if (shouldBypassFileGates) return basePreflight;
const workspaceDir = resolveAgentWorkspaceDir(params.cfg, params.agentId);
const heartbeatFilePath = path.join(workspaceDir, DEFAULT_HEARTBEAT_FILENAME);
try {
if (isHeartbeatContentEffectivelyEmpty(await fs$1.readFile(heartbeatFilePath, "utf-8"))) return {
...basePreflight,
skipReason: "empty-heartbeat-file"
};
} catch (err) {
if (hasErrnoCode(err, "ENOENT")) return basePreflight;
}
return basePreflight;
}
function appendHeartbeatWorkspacePathHint(prompt, workspaceDir) {
if (!/heartbeat\.md/i.test(prompt)) return prompt;
const hint = `When reading HEARTBEAT.md, use workspace file ${path.join(workspaceDir, DEFAULT_HEARTBEAT_FILENAME).replace(/\\/g, "/")} (exact case). Do not read docs/heartbeat.md.`;
if (prompt.includes(hint)) return prompt;
return `${prompt}\n${hint}`;
}
function resolveHeartbeatRunPrompt(params) {
const pendingEventEntries = params.preflight.pendingEventEntries;
const pendingEvents = params.preflight.shouldInspectPendingEvents ? pendingEventEntries.map((event) => event.text) : [];
const cronEvents = pendingEventEntries.filter((event) => (params.preflight.isCronEventReason || event.contextKey?.startsWith("cron:")) && isCronSystemEvent(event.text)).map((event) => event.text);
const hasExecCompletion = pendingEvents.some(isExecCompletionEvent);
const hasCronEvents = cronEvents.length > 0;
return {
prompt: appendHeartbeatWorkspacePathHint(hasExecCompletion ? buildExecEventPrompt({ deliverToUser: params.canRelayToUser }) : hasCronEvents ? buildCronEventPrompt(cronEvents, { deliverToUser: params.canRelayToUser }) : resolveHeartbeatPrompt(params.cfg, params.heartbeat), params.workspaceDir),
hasExecCompletion,
hasCronEvents
};
}
async function runHeartbeatOnce(opts) {
const cfg = opts.cfg ?? loadConfig();
const explicitAgentId = typeof opts.agentId === "string" ? opts.agentId.trim() : "";
const forcedSessionAgentId = explicitAgentId.length > 0 ? void 0 : parseAgentSessionKey(opts.sessionKey)?.agentId;
const agentId = normalizeAgentId(explicitAgentId || forcedSessionAgentId || resolveDefaultAgentId(cfg));
const heartbeat = opts.heartbeat ?? resolveHeartbeatConfig(cfg, agentId);
if (!areHeartbeatsEnabled()) return {
status: "skipped",
reason: "disabled"
};
if (!isHeartbeatEnabledForAgent(cfg, agentId)) return {
status: "skipped",
reason: "disabled"
};
if (!resolveHeartbeatIntervalMs(cfg, void 0, heartbeat)) return {
status: "skipped",
reason: "disabled"
};
const startedAt = opts.deps?.nowMs?.() ?? Date.now();
if (!isWithinActiveHours(cfg, heartbeat, startedAt)) return {
status: "skipped",
reason: "quiet-hours"
};
if ((opts.deps?.getQueueSize ?? getQueueSize)(CommandLane.Main) > 0) return {
status: "skipped",
reason: "requests-in-flight"
};
const preflight = await resolveHeartbeatPreflight({
cfg,
agentId,
heartbeat,
forcedSessionKey: opts.sessionKey,
reason: opts.reason
});
if (preflight.skipReason) {
emitHeartbeatEvent({
status: "skipped",
reason: preflight.skipReason,
durationMs: Date.now() - startedAt
});
return {
status: "skipped",
reason: preflight.skipReason
};
}
const { entry, sessionKey, storePath } = preflight.session;
const previousUpdatedAt = entry?.updatedAt;
const useIsolatedSession = heartbeat?.isolatedSession === true;
let runSessionKey = sessionKey;
let runStorePath = storePath;
if (useIsolatedSession) {
const isolatedKey = `${sessionKey}:heartbeat`;
const cronSession = resolveCronSession({
cfg,
sessionKey: isolatedKey,
agentId,
nowMs: startedAt,
forceNew: true
});
cronSession.store[isolatedKey] = cronSession.sessionEntry;
await saveSessionStore(cronSession.storePath, cronSession.store);
runSessionKey = isolatedKey;
runStorePath = cronSession.storePath;
}
const delivery = resolveHeartbeatDeliveryTarget({
cfg,
entry,
heartbeat
});
const heartbeatAccountId = heartbeat?.accountId?.trim();
if (delivery.reason === "unknown-account") log$5.warn("heartbeat: unknown accountId", {
accountId: delivery.accountId ?? heartbeatAccountId ?? null,
target: heartbeat?.target ?? "none"
});
else if (heartbeatAccountId) log$5.info("heartbeat: using explicit accountId", {
accountId: delivery.accountId ?? heartbeatAccountId,
target: heartbeat?.target ?? "none",
channel: delivery.channel
});
const visibility = delivery.channel !== "none" ? resolveHeartbeatVisibility({
cfg,
channel: delivery.channel,
accountId: delivery.accountId
}) : {
showOk: false,
showAlerts: true,
useIndicator: true
};
const { sender } = resolveHeartbeatSenderContext({
cfg,
entry,
delivery
});
const responsePrefix = resolveEffectiveMessagesConfig(cfg, agentId, {
channel: delivery.channel !== "none" ? delivery.channel : void 0,
accountId: delivery.accountId
}).responsePrefix;
const { prompt, hasExecCompletion, hasCronEvents } = resolveHeartbeatRunPrompt({
cfg,
heartbeat,
preflight,
canRelayToUser: Boolean(delivery.channel !== "none" && delivery.to && visibility.showAlerts),
workspaceDir: resolveAgentWorkspaceDir(cfg, agentId)
});
const ctx = {
Body: appendCronStyleCurrentTimeLine(prompt, cfg, startedAt),
From: sender,
To: sender,
OriginatingChannel: delivery.channel !== "none" ? delivery.channel : void 0,
OriginatingTo: delivery.to,
AccountId: delivery.accountId,
MessageThreadId: delivery.threadId,
Provider: hasExecCompletion ? "exec-event" : hasCronEvents ? "cron-event" : "heartbeat",
SessionKey: runSessionKey
};
if (!visibility.showAlerts && !visibility.showOk && !visibility.useIndicator) {
emitHeartbeatEvent({
status: "skipped",
reason: "alerts-disabled",
durationMs: Date.now() - startedAt,
channel: delivery.channel !== "none" ? delivery.channel : void 0,
accountId: delivery.accountId
});
return {
status: "skipped",
reason: "alerts-disabled"
};
}
const heartbeatOkText = responsePrefix ? `${responsePrefix} ${HEARTBEAT_TOKEN}` : HEARTBEAT_TOKEN;
const outboundSession = buildOutboundSessionContext({
cfg,
agentId,
sessionKey
});
const canAttemptHeartbeatOk = Boolean(visibility.showOk && delivery.channel !== "none" && delivery.to);
const maybeSendHeartbeatOk = async () => {
if (!canAttemptHeartbeatOk || delivery.channel === "none" || !delivery.to) return false;
const heartbeatPlugin = getChannelPlugin(delivery.channel);
if (heartbeatPlugin?.heartbeat?.checkReady) {
if (!(await heartbeatPlugin.heartbeat.checkReady({
cfg,
accountId: delivery.accountId,
deps: opts.deps
})).ok) return false;
}
await deliverOutboundPayloads({
cfg,
channel: delivery.channel,
to: delivery.to,
accountId: delivery.accountId,
threadId: delivery.threadId,
payloads: [{ text: heartbeatOkText }],
session: outboundSession,
deps: opts.deps
});
return true;
};
try {
const transcriptState = await captureTranscriptState({
storePath: runStorePath,
sessionKey: runSessionKey,
agentId
});
const heartbeatModelOverride = heartbeat?.model?.trim() || void 0;
const suppressToolErrorWarnings = heartbeat?.suppressToolErrorWarnings === true;
const bootstrapContextMode = heartbeat?.lightContext === true ? "lightweight" : void 0;
const replyResult = await getReplyFromConfig(ctx, heartbeatModelOverride ? {
isHeartbeat: true,
heartbeatModelOverride,
suppressToolErrorWarnings,
bootstrapContextMode
} : {
isHeartbeat: true,
suppressToolErrorWarnings,
bootstrapContextMode
}, cfg);
const replyPayload = resolveHeartbeatReplyPayload(replyResult);
const reasoningPayloads = heartbeat?.includeReasoning === true ? resolveHeartbeatReasoningPayloads(replyResult).filter((payload) => payload !== replyPayload) : [];
if (!replyPayload || !hasOutboundReplyContent(replyPayload)) {
await restoreHeartbeatUpdatedAt({
storePath,
sessionKey,
updatedAt: previousUpdatedAt
});
await pruneHeartbeatTranscript(transcriptState);
const okSent = await maybeSendHeartbeatOk();
emitHeartbeatEvent({
status: "ok-empty",
reason: opts.reason,
durationMs: Date.now() - startedAt,
channel: delivery.channel !== "none" ? delivery.channel : void 0,
accountId: delivery.accountId,
silent: !okSent,
indicatorType: visibility.useIndicator ? resolveIndicatorType("ok-empty") : void 0
});
return {
status: "ran",
durationMs: Date.now() - startedAt
};
}
const normalized = normalizeHeartbeatReply(replyPayload, responsePrefix, resolveHeartbeatAckMaxChars$2(cfg, heartbeat));
const execFallbackText = hasExecCompletion && !normalized.text.trim() && replyPayload.text?.trim() ? replyPayload.text.trim() : null;
if (execFallbackText) {
normalized.text = execFallbackText;
normalized.shouldSkip = false;
}
const shouldSkipMain = normalized.shouldSkip && !normalized.hasMedia && !hasExecCompletion;
if (shouldSkipMain && reasoningPayloads.length === 0) {
await restoreHeartbeatUpdatedAt({
storePath,
sessionKey,
updatedAt: previousUpdatedAt
});
await pruneHeartbeatTranscript(transcriptState);
const okSent = await maybeSendHeartbeatOk();
emitHeartbeatEvent({
status: "ok-token",
reason: opts.reason,
durationMs: Date.now() - startedAt,
channel: delivery.channel !== "none" ? delivery.channel : void 0,
accountId: delivery.accountId,
silent: !okSent,
indicatorType: visibility.useIndicator ? resolveIndicatorType("ok-token") : void 0
});
return {
status: "ran",
durationMs: Date.now() - startedAt
};
}
const mediaUrls = resolveSendableOutboundReplyParts(replyPayload).mediaUrls;
const prevHeartbeatText = typeof entry?.lastHeartbeatText === "string" ? entry.lastHeartbeatText : "";
const prevHeartbeatAt = typeof entry?.lastHeartbeatSentAt === "number" ? entry.lastHeartbeatSentAt : void 0;
if (!shouldSkipMain && !mediaUrls.length && Boolean(prevHeartbeatText.trim()) && normalized.text.trim() === prevHeartbeatText.trim() && typeof prevHeartbeatAt === "number" && startedAt - prevHeartbeatAt < 1440 * 60 * 1e3) {
await restoreHeartbeatUpdatedAt({
storePath,
sessionKey,
updatedAt: previousUpdatedAt
});
await pruneHeartbeatTranscript(transcriptState);
emitHeartbeatEvent({
status: "skipped",
reason: "duplicate",
preview: normalized.text.slice(0, 200),
durationMs: Date.now() - startedAt,
hasMedia: false,
channel: delivery.channel !== "none" ? delivery.channel : void 0,
accountId: delivery.accountId
});
return {
status: "ran",
durationMs: Date.now() - startedAt
};
}
const previewText = shouldSkipMain ? reasoningPayloads.map((payload) => payload.text).filter((text) => Boolean(text?.trim())).join("\n") : normalized.text;
if (delivery.channel === "none" || !delivery.to) {
emitHeartbeatEvent({
status: "skipped",
reason: delivery.reason ?? "no-target",
preview: previewText?.slice(0, 200),
durationMs: Date.now() - startedAt,
hasMedia: mediaUrls.length > 0,
accountId: delivery.accountId
});
return {
status: "ran",
durationMs: Date.now() - startedAt
};
}
if (!visibility.showAlerts) {
await restoreHeartbeatUpdatedAt({
storePath,
sessionKey,
updatedAt: previousUpdatedAt
});
emitHeartbeatEvent({
status: "skipped",
reason: "alerts-disabled",
preview: previewText?.slice(0, 200),
durationMs: Date.now() - startedAt,
channel: delivery.channel,
hasMedia: mediaUrls.length > 0,
accountId: delivery.accountId,
indicatorType: visibility.useIndicator ? resolveIndicatorType("sent") : void 0
});
return {
status: "ran",
durationMs: Date.now() - startedAt
};
}
const deliveryAccountId = delivery.accountId;
const heartbeatPlugin = getChannelPlugin(delivery.channel);
if (heartbeatPlugin?.heartbeat?.checkReady) {
const readiness = await heartbeatPlugin.heartbeat.checkReady({
cfg,
accountId: deliveryAccountId,
deps: opts.deps
});
if (!readiness.ok) {
emitHeartbeatEvent({
status: "skipped",
reason: readiness.reason,
preview: previewText?.slice(0, 200),
durationMs: Date.now() - startedAt,
hasMedia: mediaUrls.length > 0,
channel: delivery.channel,
accountId: delivery.accountId
});
log$5.info("heartbeat: channel not ready", {
channel: delivery.channel,
reason: readiness.reason
});
return {
status: "skipped",
reason: readiness.reason
};
}
}
await deliverOutboundPayloads({
cfg,
channel: delivery.channel,
to: delivery.to,
accountId: deliveryAccountId,
session: outboundSession,
threadId: delivery.threadId,
payloads: [...reasoningPayloads, ...shouldSkipMain ? [] : [{
text: normalized.text,
mediaUrls
}]],
deps: opts.deps
});
if (!shouldSkipMain && normalized.text.trim()) {
const store = loadSessionStore(storePath);
const current = store[sessionKey];
if (current) {
store[sessionKey] = {
...current,
lastHeartbeatText: normalized.text,
lastHeartbeatSentAt: startedAt
};
await saveSessionStore(storePath, store);
}
}
emitHeartbeatEvent({
status: "sent",
to: delivery.to,
preview: previewText?.slice(0, 200),
durationMs: Date.now() - startedAt,
hasMedia: mediaUrls.length > 0,
channel: delivery.channel,
accountId: delivery.accountId,
indicatorType: visibility.useIndicator ? resolveIndicatorType("sent") : void 0
});
return {
status: "ran",
durationMs: Date.now() - startedAt
};
} catch (err) {
const reason = formatErrorMessage(err);
emitHeartbeatEvent({
status: "failed",
reason,
durationMs: Date.now() - startedAt,
channel: delivery.channel !== "none" ? delivery.channel : void 0,
accountId: delivery.accountId,
indicatorType: visibility.useIndicator ? resolveIndicatorType("failed") : void 0
});
log$5.error(`heartbeat failed: ${reason}`, { error: reason });
return {
status: "failed",
reason
};
}
}
function startHeartbeatRunner(opts) {
const runtime = opts.runtime ?? defaultRuntime;
const runOnce = opts.runOnce ?? runHeartbeatOnce;
const state = {
cfg: opts.cfg ?? loadConfig(),
runtime,
agents: /* @__PURE__ */ new Map(),
timer: null,
stopped: false
};
let initialized = false;
const resolveNextDue = (now, intervalMs, prevState) => {
if (typeof prevState?.lastRunMs === "number") return prevState.lastRunMs + intervalMs;
if (prevState && prevState.intervalMs === intervalMs && prevState.nextDueMs > now) return prevState.nextDueMs;
return now + intervalMs;
};
const advanceAgentSchedule = (agent, now) => {
agent.lastRunMs = now;
agent.nextDueMs = now + agent.intervalMs;
};
const scheduleNext = () => {
if (state.stopped) return;
if (state.timer) {
clearTimeout(state.timer);
state.timer = null;
}
if (state.agents.size === 0) return;
const now = Date.now();
let nextDue = Number.POSITIVE_INFINITY;
for (const agent of state.agents.values()) if (agent.nextDueMs < nextDue) nextDue = agent.nextDueMs;
if (!Number.isFinite(nextDue)) return;
const delay = Math.max(0, nextDue - now);
state.timer = setTimeout(() => {
state.timer = null;
requestHeartbeatNow({
reason: "interval",
coalesceMs: 0
});
}, delay);
state.timer.unref?.();
};
const updateConfig = (cfg) => {
if (state.stopped) return;
const now = Date.now();
const prevAgents = state.agents;
const prevEnabled = prevAgents.size > 0;
const nextAgents = /* @__PURE__ */ new Map();
const intervals = [];
for (const agent of resolveHeartbeatAgents(cfg)) {
const intervalMs = resolveHeartbeatIntervalMs(cfg, void 0, agent.heartbeat);
if (!intervalMs) continue;
intervals.push(intervalMs);
const prevState = prevAgents.get(agent.agentId);
const nextDueMs = resolveNextDue(now, intervalMs, prevState);
nextAgents.set(agent.agentId, {
agentId: agent.agentId,
heartbeat: agent.heartbeat,
intervalMs,
lastRunMs: prevState?.lastRunMs,
nextDueMs
});
}
state.cfg = cfg;
state.agents = nextAgents;
const nextEnabled = nextAgents.size > 0;
if (!initialized) {
if (!nextEnabled) log$5.info("heartbeat: disabled", { enabled: false });
else log$5.info("heartbeat: started", { intervalMs: Math.min(...intervals) });
initialized = true;
} else if (prevEnabled !== nextEnabled) if (!nextEnabled) log$5.info("heartbeat: disabled", { enabled: false });
else log$5.info("heartbeat: started", { intervalMs: Math.min(...intervals) });
scheduleNext();
};
const run = async (params) => {
if (state.stopped) return {
status: "skipped",
reason: "disabled"
};
if (!areHeartbeatsEnabled()) return {
status: "skipped",
reason: "disabled"
};
if (state.agents.size === 0) return {
status: "skipped",
reason: "disabled"
};
const reason = params?.reason;
const requestedAgentId = params?.agentId ? normalizeAgentId(params.agentId) : void 0;
const requestedSessionKey = params?.sessionKey?.trim() || void 0;
const isInterval = reason === "interval";
const startedAt = Date.now();
const now = startedAt;
let ran = false;
if (requestedSessionKey || requestedAgentId) {
const targetAgentId = requestedAgentId ?? resolveAgentIdFromSessionKey(requestedSessionKey);
const targetAgent = state.agents.get(targetAgentId);
if (!targetAgent) {
scheduleNext();
return {
status: "skipped",
reason: "disabled"
};
}
try {
const res = await runOnce({
cfg: state.cfg,
agentId: targetAgent.agentId,
heartbeat: targetAgent.heartbeat,
reason,
sessionKey: requestedSessionKey,
deps: { runtime: state.runtime }
});
if (res.status !== "skipped" || res.reason !== "disabled") advanceAgentSchedule(targetAgent, now);
scheduleNext();
return res.status === "ran" ? {
status: "ran",
durationMs: Date.now() - startedAt
} : res;
} catch (err) {
const errMsg = formatErrorMessage(err);
log$5.error(`heartbeat runner: targeted runOnce threw unexpectedly: ${errMsg}`, { error: errMsg });
advanceAgentSchedule(targetAgent, now);
scheduleNext();
return {
status: "failed",
reason: errMsg
};
}
}
for (const agent of state.agents.values()) {
if (isInterval && now < agent.nextDueMs) continue;
let res;
try {
res = await runOnce({
cfg: state.cfg,
agentId: agent.agentId,
heartbeat: agent.heartbeat,
reason,
deps: { runtime: state.runtime }
});
} catch (err) {
const errMsg = formatErrorMessage(err);
log$5.error(`heartbeat runner: runOnce threw unexpectedly: ${errMsg}`, { error: errMsg });
advanceAgentSchedule(agent, now);
continue;
}
if (res.status === "skipped" && res.reason === "requests-in-flight") return res;
if (res.status !== "skipped" || res.reason !== "disabled") advanceAgentSchedule(agent, now);
if (res.status === "ran") ran = true;
}
scheduleNext();
if (ran) return {
status: "ran",
durationMs: Date.now() - startedAt
};
return {
status: "skipped",
reason: isInterval ? "not-due" : "disabled"
};
};
const wakeHandler = async (params) => run({
reason: params.reason,
agentId: params.agentId,
sessionKey: params.sessionKey
});
const disposeWakeHandler = setHeartbeatWakeHandler(wakeHandler);
updateConfig(state.cfg);
const cleanup = () => {
if (state.stopped) return;
state.stopped = true;
disposeWakeHandler();
if (state.timer) clearTimeout(state.timer);
state.timer = null;
};
opts.abortSignal?.addEventListener("abort", cleanup, { once: true });
return {
stop: cleanup,
updateConfig
};
}
//#endregion
//#region src/infra/update-startup.ts
let updateAvailableCache = null;
function getUpdateAvailable() {
return updateAvailableCache;
}
const UPDATE_CHECK_FILENAME = "update-check.json";
const UPDATE_CHECK_INTERVAL_MS = 1440 * 60 * 1e3;
const ONE_HOUR_MS$1 = 3600 * 1e3;
const AUTO_UPDATE_COMMAND_TIMEOUT_MS = 2700 * 1e3;
const AUTO_STABLE_DELAY_HOURS_DEFAULT = 6;
const AUTO_STABLE_JITTER_HOURS_DEFAULT = 12;
const AUTO_BETA_CHECK_INTERVAL_HOURS_DEFAULT = 1;
function shouldSkipCheck(allowInTests) {
if (allowInTests) return false;
if (process.env.VITEST || false) return true;
return false;
}
function resolveAutoUpdatePolicy(cfg) {
const auto = cfg.update?.auto;
const stableDelayHours = typeof auto?.stableDelayHours === "number" && Number.isFinite(auto.stableDelayHours) ? Math.max(0, auto.stableDelayHours) : AUTO_STABLE_DELAY_HOURS_DEFAULT;
const stableJitterHours = typeof auto?.stableJitterHours === "number" && Number.isFinite(auto.stableJitterHours) ? Math.max(0, auto.stableJitterHours) : AUTO_STABLE_JITTER_HOURS_DEFAULT;
const betaCheckIntervalHours = typeof auto?.betaCheckIntervalHours === "number" && Number.isFinite(auto.betaCheckIntervalHours) ? Math.max(.25, auto.betaCheckIntervalHours) : AUTO_BETA_CHECK_INTERVAL_HOURS_DEFAULT;
return {
enabled: Boolean(auto?.enabled),
stableDelayHours,
stableJitterHours,
betaCheckIntervalHours
};
}
function resolveCheckIntervalMs(cfg) {
const channel = normalizeUpdateChannel(cfg.update?.channel) ?? "stable";
const auto = resolveAutoUpdatePolicy(cfg);
if (!auto.enabled) return UPDATE_CHECK_INTERVAL_MS;
if (channel === "beta") return Math.max(ONE_HOUR_MS$1 / 4, Math.floor(auto.betaCheckIntervalHours * ONE_HOUR_MS$1));
if (channel === "stable") return ONE_HOUR_MS$1;
return UPDATE_CHECK_INTERVAL_MS;
}
async function readState(statePath) {
try {
const raw = await fs$1.readFile(statePath, "utf-8");
const parsed = JSON.parse(raw);
return parsed && typeof parsed === "object" ? parsed : {};
} catch {
return {};
}
}
async function writeState(statePath, state) {
await writeJsonAtomic(statePath, state);
}
function sameUpdateAvailable(a, b) {
if (a === b) return true;
if (!a || !b) return false;
return a.currentVersion === b.currentVersion && a.latestVersion === b.latestVersion && a.channel === b.channel;
}
function setUpdateAvailableCache(params) {
if (sameUpdateAvailable(updateAvailableCache, params.next)) return;
updateAvailableCache = params.next;
params.onUpdateAvailableChange?.(params.next);
}
function resolvePersistedUpdateAvailable(state) {
const latestVersion = state.lastAvailableVersion?.trim();
if (!latestVersion) return null;
const cmp = compareSemverStrings(VERSION, latestVersion);
if (cmp == null || cmp >= 0) return null;
return {
currentVersion: VERSION,
latestVersion,
channel: state.lastAvailableTag?.trim() || "stable"
};
}
function resolveStableJitterMs(params) {
if (params.jitterWindowMs <= 0) return 0;
return createHash("sha256").update(`${params.installId}:${params.version}:${params.tag}`).digest().readUInt32BE(0) % (Math.floor(params.jitterWindowMs) + 1);
}
function resolveStableAutoApplyAtMs(params) {
if (!params.nextState.autoInstallId) params.nextState.autoInstallId = params.state.autoInstallId?.trim() || randomUUID();
const installId = params.nextState.autoInstallId;
if (!(params.state.autoFirstSeenVersion === params.version && params.state.autoFirstSeenTag === params.tag)) {
params.nextState.autoFirstSeenVersion = params.version;
params.nextState.autoFirstSeenTag = params.tag;
params.nextState.autoFirstSeenAt = new Date(params.nowMs).toISOString();
} else {
params.nextState.autoFirstSeenVersion = params.state.autoFirstSeenVersion;
params.nextState.autoFirstSeenTag = params.state.autoFirstSeenTag;
params.nextState.autoFirstSeenAt = params.state.autoFirstSeenAt;
}
const firstSeenMs = params.nextState.autoFirstSeenAt ? Date.parse(params.nextState.autoFirstSeenAt) : params.nowMs;
const baseDelayMs = Math.max(0, params.stableDelayHours) * ONE_HOUR_MS$1;
const jitterWindowMs = Math.max(0, params.stableJitterHours) * ONE_HOUR_MS$1;
const jitterMs = resolveStableJitterMs({
installId,
version: params.version,
tag: params.tag,
jitterWindowMs
});
return firstSeenMs + baseDelayMs + jitterMs;
}
async function runAutoUpdateCommand(params) {
const baseArgs = [
"update",
"--yes",
"--channel",
params.channel,
"--json"
];
const execPath = process.execPath?.trim();
const argv1 = process.argv[1]?.trim();
const lowerExecBase = execPath ? path.basename(execPath).toLowerCase() : "";
const runtimeIsNodeOrBun = lowerExecBase === "node" || lowerExecBase === "node.exe" || lowerExecBase === "bun" || lowerExecBase === "bun.exe";
const argv = [];
if (execPath && argv1) argv.push(execPath, argv1, ...baseArgs);
else if (execPath && !runtimeIsNodeOrBun) argv.push(execPath, ...baseArgs);
else if (execPath && params.root) {
const candidates = [
path.join(params.root, "dist", "entry.js"),
path.join(params.root, "dist", "entry.mjs"),
path.join(params.root, "dist", "index.js"),
path.join(params.root, "dist", "index.mjs")
];
for (const candidate of candidates) try {
await fs$1.access(candidate);
argv.push(execPath, candidate, ...baseArgs);
break;
} catch {}
}
if (argv.length === 0) argv.push("openclaw", ...baseArgs);
try {
const res = await runCommandWithTimeout(argv, {
timeoutMs: params.timeoutMs,
env: { OPENCLAW_AUTO_UPDATE: "1" }
});
return {
ok: res.code === 0,
code: res.code,
stdout: res.stdout,
stderr: res.stderr,
reason: res.code === 0 ? void 0 : "non-zero-exit"
};
} catch (err) {
return {
ok: false,
code: null,
reason: String(err)
};
}
}
function clearAutoState(nextState) {
delete nextState.autoFirstSeenVersion;
delete nextState.autoFirstSeenTag;
delete nextState.autoFirstSeenAt;
}
async function runGatewayUpdateCheck(params) {
if (shouldSkipCheck(Boolean(params.allowInTests))) return;
if (params.isNixMode) return;
const auto = resolveAutoUpdatePolicy(params.cfg);
const shouldRunUpdateHints = params.cfg.update?.checkOnStart !== false;
if (!shouldRunUpdateHints && !auto.enabled) return;
const statePath = path.join(resolveStateDir(), UPDATE_CHECK_FILENAME);
const state = await readState(statePath);
const now = Date.now();
const lastCheckedAt = state.lastCheckedAt ? Date.parse(state.lastCheckedAt) : null;
if (shouldRunUpdateHints) setUpdateAvailableCache({
next: resolvePersistedUpdateAvailable(state),
onUpdateAvailableChange: params.onUpdateAvailableChange
});
else setUpdateAvailableCache({
next: null,
onUpdateAvailableChange: params.onUpdateAvailableChange
});
const checkIntervalMs = resolveCheckIntervalMs(params.cfg);
if (lastCheckedAt && Number.isFinite(lastCheckedAt)) {
if (now - lastCheckedAt < checkIntervalMs) return;
}
const root = await resolveOpenClawPackageRoot({
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
});
const status = await checkUpdateStatus({
root,
timeoutMs: 2500,
fetchGit: false,
includeRegistry: false
});
const nextState = {
...state,
lastCheckedAt: new Date(now).toISOString()
};
if (status.installKind !== "package") {
delete nextState.lastAvailableVersion;
delete nextState.lastAvailableTag;
clearAutoState(nextState);
setUpdateAvailableCache({
next: null,
onUpdateAvailableChange: params.onUpdateAvailableChange
});
await writeState(statePath, nextState);
return;
}
const channel = normalizeUpdateChannel(params.cfg.update?.channel) ?? "stable";
const resolved = await resolveNpmChannelTag({
channel,
timeoutMs: 2500
});
const tag = resolved.tag;
if (!resolved.version) {
await writeState(statePath, nextState);
return;
}
const cmp = compareSemverStrings(VERSION, resolved.version);
if (cmp != null && cmp < 0) {
const nextAvailable = {
currentVersion: VERSION,
latestVersion: resolved.version,
channel: tag
};
if (shouldRunUpdateHints) setUpdateAvailableCache({
next: nextAvailable,
onUpdateAvailableChange: params.onUpdateAvailableChange
});
nextState.lastAvailableVersion = resolved.version;
nextState.lastAvailableTag = tag;
const shouldNotify = state.lastNotifiedVersion !== resolved.version || state.lastNotifiedTag !== tag;
if (shouldRunUpdateHints && shouldNotify) {
params.log.info(`update available (${tag}): v${resolved.version} (current v${VERSION}). Run: ${formatCliCommand("openclaw update")}`);
nextState.lastNotifiedVersion = resolved.version;
nextState.lastNotifiedTag = tag;
}
if (auto.enabled && (channel === "stable" || channel === "beta")) {
const runAuto = params.runAutoUpdate ?? runAutoUpdateCommand;
const attemptIntervalMs = channel === "beta" ? Math.max(ONE_HOUR_MS$1 / 4, Math.floor(auto.betaCheckIntervalHours * ONE_HOUR_MS$1)) : ONE_HOUR_MS$1;
const lastAttemptAt = state.autoLastAttemptAt ? Date.parse(state.autoLastAttemptAt) : null;
const recentAttemptForSameVersion = state.autoLastAttemptVersion === resolved.version && lastAttemptAt != null && Number.isFinite(lastAttemptAt) && now - lastAttemptAt < attemptIntervalMs;
let dueNow = channel === "beta";
let applyAfterMs = null;
if (channel === "stable") {
applyAfterMs = resolveStableAutoApplyAtMs({
state,
nextState,
nowMs: now,
version: resolved.version,
tag,
stableDelayHours: auto.stableDelayHours,
stableJitterHours: auto.stableJitterHours
});
dueNow = now >= applyAfterMs;
}
if (!dueNow) params.log.info("auto-update deferred (stable rollout window active)", {
version: resolved.version,
tag,
applyAfter: applyAfterMs ? new Date(applyAfterMs).toISOString() : void 0
});
else if (recentAttemptForSameVersion) params.log.info("auto-update deferred (recent attempt exists)", {
version: resolved.version,
tag
});
else {
nextState.autoLastAttemptVersion = resolved.version;
nextState.autoLastAttemptAt = new Date(now).toISOString();
const outcome = await runAuto({
channel,
timeoutMs: AUTO_UPDATE_COMMAND_TIMEOUT_MS,
root: root ?? void 0
});
if (outcome.ok) {
nextState.autoLastSuccessVersion = resolved.version;
nextState.autoLastSuccessAt = new Date(now).toISOString();
params.log.info("auto-update applied", {
channel,
version: resolved.version,
tag
});
} else params.log.info("auto-update attempt failed", {
channel,
version: resolved.version,
tag,
reason: outcome.reason ?? `exit:${outcome.code}`
});
}
}
} else {
delete nextState.lastAvailableVersion;
delete nextState.lastAvailableTag;
clearAutoState(nextState);
setUpdateAvailableCache({
next: null,
onUpdateAvailableChange: params.onUpdateAvailableChange
});
}
await writeState(statePath, nextState);
}
function scheduleGatewayUpdateCheck(params) {
let stopped = false;
let timer = null;
let running = false;
const tick = async () => {
if (stopped || running) return;
running = true;
try {
await runGatewayUpdateCheck(params);
} catch {} finally {
running = false;
}
if (stopped) return;
const intervalMs = resolveCheckIntervalMs(params.cfg);
timer = setTimeout(() => {
tick();
}, intervalMs);
};
tick();
return () => {
stopped = true;
if (timer) {
clearTimeout(timer);
timer = null;
}
};
}
//#endregion
//#region src/gateway/channel-health-policy.ts
function isManagedAccount(snapshot) {
return snapshot.enabled !== false && snapshot.configured !== false;
}
const BUSY_ACTIVITY_STALE_THRESHOLD_MS = 25 * 6e4;
const DEFAULT_CHANNEL_STALE_EVENT_THRESHOLD_MS = 30 * 6e4;
const DEFAULT_CHANNEL_CONNECT_GRACE_MS = 12e4;
function evaluateChannelHealth(snapshot, policy) {
if (!isManagedAccount(snapshot)) return {
healthy: true,
reason: "unmanaged"
};
if (!snapshot.running) return {
healthy: false,
reason: "not-running"
};
const activeRuns = typeof snapshot.activeRuns === "number" && Number.isFinite(snapshot.activeRuns) ? Math.max(0, Math.trunc(snapshot.activeRuns)) : 0;
const isBusy = snapshot.busy === true || activeRuns > 0;
const lastStartAt = typeof snapshot.lastStartAt === "number" && Number.isFinite(snapshot.lastStartAt) ? snapshot.lastStartAt : null;
const lastRunActivityAt = typeof snapshot.lastRunActivityAt === "number" && Number.isFinite(snapshot.lastRunActivityAt) ? snapshot.lastRunActivityAt : null;
const busyStateInitializedForLifecycle = lastStartAt == null || lastRunActivityAt != null && lastRunActivityAt >= lastStartAt;
if (isBusy) if (!busyStateInitializedForLifecycle) {} else {
if ((lastRunActivityAt == null ? Number.POSITIVE_INFINITY : Math.max(0, policy.now - lastRunActivityAt)) < BUSY_ACTIVITY_STALE_THRESHOLD_MS) return {
healthy: true,
reason: "busy"
};
return {
healthy: false,
reason: "stuck"
};
}
if (snapshot.lastStartAt != null) {
if (policy.now - snapshot.lastStartAt < policy.channelConnectGraceMs) return {
healthy: true,
reason: "startup-connect-grace"
};
}
if (snapshot.connected === false) return {
healthy: false,
reason: "disconnected"
};
if (policy.channelId !== "telegram" && snapshot.mode !== "webhook" && snapshot.connected === true && snapshot.lastEventAt != null) {
if (lastStartAt != null && snapshot.lastEventAt < lastStartAt) {
if (Math.max(0, policy.now - lastStartAt) <= policy.staleEventThresholdMs) return {
healthy: true,
reason: "healthy"
};
return {
healthy: false,
reason: "stale-socket"
};
}
if (policy.now - snapshot.lastEventAt > policy.staleEventThresholdMs) return {
healthy: false,
reason: "stale-socket"
};
}
return {
healthy: true,
reason: "healthy"
};
}
function resolveChannelRestartReason(snapshot, evaluation) {
if (evaluation.reason === "stale-socket") return "stale-socket";
if (evaluation.reason === "not-running") return snapshot.reconnectAttempts && snapshot.reconnectAttempts >= 10 ? "gave-up" : "stopped";
if (evaluation.reason === "disconnected") return "disconnected";
return "stuck";
}
//#endregion
//#region src/gateway/channel-health-monitor.ts
const log$4 = createSubsystemLogger("gateway/health-monitor");
const DEFAULT_CHECK_INTERVAL_MS = 5 * 6e4;
const DEFAULT_MONITOR_STARTUP_GRACE_MS = 6e4;
const DEFAULT_COOLDOWN_CYCLES = 2;
const DEFAULT_MAX_RESTARTS_PER_HOUR = 10;
const ONE_HOUR_MS = 60 * 6e4;
function resolveTimingPolicy(deps) {
return {
monitorStartupGraceMs: deps.timing?.monitorStartupGraceMs ?? deps.startupGraceMs ?? DEFAULT_MONITOR_STARTUP_GRACE_MS,
channelConnectGraceMs: deps.timing?.channelConnectGraceMs ?? deps.channelStartupGraceMs ?? 12e4,
staleEventThresholdMs: deps.timing?.staleEventThresholdMs ?? deps.staleEventThresholdMs ?? 18e5
};
}
function startChannelHealthMonitor(deps) {
const { channelManager, checkIntervalMs = DEFAULT_CHECK_INTERVAL_MS, cooldownCycles = DEFAULT_COOLDOWN_CYCLES, maxRestartsPerHour = DEFAULT_MAX_RESTARTS_PER_HOUR, abortSignal } = deps;
const timing = resolveTimingPolicy(deps);
const cooldownMs = cooldownCycles * checkIntervalMs;
const restartRecords = /* @__PURE__ */ new Map();
const startedAt = Date.now();
let stopped = false;
let checkInFlight = false;
let timer = null;
const rKey = (channelId, accountId) => `${channelId}:${accountId}`;
function pruneOldRestarts(record, now) {
record.restartsThisHour = record.restartsThisHour.filter((r) => now - r.at < ONE_HOUR_MS);
}
async function runCheck() {
if (stopped || checkInFlight) return;
checkInFlight = true;
try {
const now = Date.now();
if (now - startedAt < timing.monitorStartupGraceMs) return;
const snapshot = channelManager.getRuntimeSnapshot();
for (const [channelId, accounts] of Object.entries(snapshot.channelAccounts)) {
if (!accounts) continue;
for (const [accountId, status] of Object.entries(accounts)) {
if (!status) continue;
if (!channelManager.isHealthMonitorEnabled(channelId, accountId)) continue;
if (channelManager.isManuallyStopped(channelId, accountId)) continue;
const health = evaluateChannelHealth(status, {
channelId,
now,
staleEventThresholdMs: timing.staleEventThresholdMs,
channelConnectGraceMs: timing.channelConnectGraceMs
});
if (health.healthy) continue;
const key = rKey(channelId, accountId);
const record = restartRecords.get(key) ?? {
lastRestartAt: 0,
restartsThisHour: []
};
if (now - record.lastRestartAt <= cooldownMs) continue;
pruneOldRestarts(record, now);
if (record.restartsThisHour.length >= maxRestartsPerHour) {
log$4.warn?.(`[${channelId}:${accountId}] health-monitor: hit ${maxRestartsPerHour} restarts/hour limit, skipping`);
continue;
}
const reason = resolveChannelRestartReason(status, health);
log$4.info?.(`[${channelId}:${accountId}] health-monitor: restarting (reason: ${reason})`);
try {
if (status.running) await channelManager.stopChannel(channelId, accountId);
channelManager.resetRestartAttempts(channelId, accountId);
await channelManager.startChannel(channelId, accountId);
record.lastRestartAt = now;
record.restartsThisHour.push({ at: now });
restartRecords.set(key, record);
} catch (err) {
log$4.error?.(`[${channelId}:${accountId}] health-monitor: restart failed: ${String(err)}`);
}
}
}
} finally {
checkInFlight = false;
}
}
function stop() {
stopped = true;
if (timer) {
clearInterval(timer);
timer = null;
}
}
if (abortSignal?.aborted) stopped = true;
else {
abortSignal?.addEventListener("abort", stop, { once: true });
timer = setInterval(() => void runCheck(), checkIntervalMs);
if (typeof timer === "object" && "unref" in timer) timer.unref();
log$4.info?.(`started (interval: ${Math.round(checkIntervalMs / 1e3)}s, startup-grace: ${Math.round(timing.monitorStartupGraceMs / 1e3)}s, channel-connect-grace: ${Math.round(timing.channelConnectGraceMs / 1e3)}s)`);
}
return { stop };
}
//#endregion
//#region src/gateway/config-reload-plan.ts
const BASE_RELOAD_RULES = [
{
prefix: "gateway.remote",
kind: "none"
},
{
prefix: "gateway.reload",
kind: "none"
},
{
prefix: "gateway.channelHealthCheckMinutes",
kind: "hot",
actions: ["restart-health-monitor"]
},
{
prefix: "gateway.channelStaleEventThresholdMinutes",
kind: "hot",
actions: ["restart-health-monitor"]
},
{
prefix: "gateway.channelMaxRestartsPerHour",
kind: "hot",
actions: ["restart-health-monitor"]
},
{
prefix: "diagnostics.stuckSessionWarnMs",
kind: "none"
},
{
prefix: "hooks.gmail",
kind: "hot",
actions: ["restart-gmail-watcher"]
},
{
prefix: "hooks",
kind: "hot",
actions: ["reload-hooks"]
},
{
prefix: "agents.defaults.heartbeat",
kind: "hot",
actions: ["restart-heartbeat"]
},
{
prefix: "agents.defaults.models",
kind: "hot",
actions: ["restart-heartbeat"]
},
{
prefix: "agents.defaults.model",
kind: "hot",
actions: ["restart-heartbeat"]
},
{
prefix: "models",
kind: "hot",
actions: ["restart-heartbeat"]
},
{
prefix: "agent.heartbeat",
kind: "hot",
actions: ["restart-heartbeat"]
},
{
prefix: "cron",
kind: "hot",
actions: ["restart-cron"]
},
{
prefix: "browser",
kind: "hot",
actions: ["restart-browser-control"]
}
];
const BASE_RELOAD_RULES_TAIL = [
{
prefix: "meta",
kind: "none"
},
{
prefix: "identity",
kind: "none"
},
{
prefix: "wizard",
kind: "none"
},
{
prefix: "logging",
kind: "none"
},
{
prefix: "agents",
kind: "none"
},
{
prefix: "tools",
kind: "none"
},
{
prefix: "bindings",
kind: "none"
},
{
prefix: "audio",
kind: "none"
},
{
prefix: "agent",
kind: "none"
},
{
prefix: "routing",
kind: "none"
},
{
prefix: "messages",
kind: "none"
},
{
prefix: "session",
kind: "none"
},
{
prefix: "talk",
kind: "none"
},
{
prefix: "skills",
kind: "none"
},
{
prefix: "secrets",
kind: "none"
},
{
prefix: "plugins",
kind: "restart"
},
{
prefix: "ui",
kind: "none"
},
{
prefix: "gateway",
kind: "restart"
},
{
prefix: "discovery",
kind: "restart"
},
{
prefix: "canvasHost",
kind: "restart"
}
];
let cachedReloadRules = null;
let cachedRegistry = null;
function listReloadRules() {
const registry = getActivePluginRegistry();
if (registry !== cachedRegistry) {
cachedReloadRules = null;
cachedRegistry = registry;
}
if (cachedReloadRules) return cachedReloadRules;
const channelReloadRules = listChannelPlugins().flatMap((plugin) => [...(plugin.reload?.configPrefixes ?? []).map((prefix) => ({
prefix,
kind: "hot",
actions: [`restart-channel:${plugin.id}`]
})), ...(plugin.reload?.noopPrefixes ?? []).map((prefix) => ({
prefix,
kind: "none"
}))]);
const rules = [
...BASE_RELOAD_RULES,
...channelReloadRules,
...BASE_RELOAD_RULES_TAIL
];
cachedReloadRules = rules;
return rules;
}
function matchRule(path) {
for (const rule of listReloadRules()) if (path === rule.prefix || path.startsWith(`${rule.prefix}.`)) return rule;
return null;
}
function buildGatewayReloadPlan(changedPaths) {
const plan = {
changedPaths,
restartGateway: false,
restartReasons: [],
hotReasons: [],
reloadHooks: false,
restartGmailWatcher: false,
restartBrowserControl: false,
restartCron: false,
restartHeartbeat: false,
restartHealthMonitor: false,
restartChannels: /* @__PURE__ */ new Set(),
noopPaths: []
};
const applyAction = (action) => {
if (action.startsWith("restart-channel:")) {
const channel = action.slice(16);
plan.restartChannels.add(channel);
return;
}
switch (action) {
case "reload-hooks":
plan.reloadHooks = true;
break;
case "restart-gmail-watcher":
plan.restartGmailWatcher = true;
break;
case "restart-browser-control":
plan.restartBrowserControl = true;
break;
case "restart-cron":
plan.restartCron = true;
break;
case "restart-heartbeat":
plan.restartHeartbeat = true;
break;
case "restart-health-monitor":
plan.restartHealthMonitor = true;
break;
default: break;
}
};
for (const path of changedPaths) {
const rule = matchRule(path);
if (!rule) {
plan.restartGateway = true;
plan.restartReasons.push(path);
continue;
}
if (rule.kind === "restart") {
plan.restartGateway = true;
plan.restartReasons.push(path);
continue;
}
if (rule.kind === "none") {
plan.noopPaths.push(path);
continue;
}
plan.hotReasons.push(path);
for (const action of rule.actions ?? []) applyAction(action);
}
if (plan.restartGmailWatcher) plan.reloadHooks = true;
return plan;
}
//#endregion
//#region src/gateway/config-reload.ts
const DEFAULT_RELOAD_SETTINGS = {
mode: "hybrid",
debounceMs: 300
};
const MISSING_CONFIG_RETRY_DELAY_MS = 150;
const MISSING_CONFIG_MAX_RETRIES = 2;
function diffConfigPaths(prev, next, prefix = "") {
if (prev === next) return [];
if (isPlainObject(prev) && isPlainObject(next)) {
const keys = new Set([...Object.keys(prev), ...Object.keys(next)]);
const paths = [];
for (const key of keys) {
const prevValue = prev[key];
const nextValue = next[key];
if (prevValue === void 0 && nextValue === void 0) continue;
const childPaths = diffConfigPaths(prevValue, nextValue, prefix ? `${prefix}.${key}` : key);
if (childPaths.length > 0) paths.push(...childPaths);
}
return paths;
}
if (Array.isArray(prev) && Array.isArray(next)) {
if (isDeepStrictEqual(prev, next)) return [];
}
return [prefix || "<root>"];
}
function resolveGatewayReloadSettings(cfg) {
const rawMode = cfg.gateway?.reload?.mode;
const mode = rawMode === "off" || rawMode === "restart" || rawMode === "hot" || rawMode === "hybrid" ? rawMode : DEFAULT_RELOAD_SETTINGS.mode;
const debounceRaw = cfg.gateway?.reload?.debounceMs;
return {
mode,
debounceMs: typeof debounceRaw === "number" && Number.isFinite(debounceRaw) ? Math.max(0, Math.floor(debounceRaw)) : DEFAULT_RELOAD_SETTINGS.debounceMs
};
}
function startGatewayConfigReloader(opts) {
let currentConfig = opts.initialConfig;
let settings = resolveGatewayReloadSettings(currentConfig);
let debounceTimer = null;
let pending = false;
let running = false;
let stopped = false;
let restartQueued = false;
let missingConfigRetries = 0;
const scheduleAfter = (wait) => {
if (stopped) return;
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = setTimeout(() => {
runReload();
}, wait);
};
const schedule = () => {
scheduleAfter(settings.debounceMs);
};
const queueRestart = (plan, nextConfig) => {
if (restartQueued) return;
restartQueued = true;
(async () => {
try {
await opts.onRestart(plan, nextConfig);
} catch (err) {
restartQueued = false;
opts.log.error(`config restart failed: ${String(err)}`);
}
})();
};
const handleMissingSnapshot = (snapshot) => {
if (snapshot.exists) {
missingConfigRetries = 0;
return false;
}
if (missingConfigRetries < MISSING_CONFIG_MAX_RETRIES) {
missingConfigRetries += 1;
opts.log.info(`config reload retry (${missingConfigRetries}/${MISSING_CONFIG_MAX_RETRIES}): config file not found`);
scheduleAfter(MISSING_CONFIG_RETRY_DELAY_MS);
return true;
}
opts.log.warn("config reload skipped (config file not found)");
return true;
};
const handleInvalidSnapshot = (snapshot) => {
if (snapshot.valid) return false;
const issues = formatConfigIssueLines(snapshot.issues, "").join(", ");
opts.log.warn(`config reload skipped (invalid config): ${issues}`);
return true;
};
const applySnapshot = async (nextConfig) => {
const changedPaths = diffConfigPaths(currentConfig, nextConfig);
currentConfig = nextConfig;
settings = resolveGatewayReloadSettings(nextConfig);
if (changedPaths.length === 0) return;
opts.log.info(`config change detected; evaluating reload (${changedPaths.join(", ")})`);
const plan = buildGatewayReloadPlan(changedPaths);
if (settings.mode === "off") {
opts.log.info("config reload disabled (gateway.reload.mode=off)");
return;
}
if (settings.mode === "restart") {
queueRestart(plan, nextConfig);
return;
}
if (plan.restartGateway) {
if (settings.mode === "hot") {
opts.log.warn(`config reload requires gateway restart; hot mode ignoring (${plan.restartReasons.join(", ")})`);
return;
}
queueRestart(plan, nextConfig);
return;
}
await opts.onHotReload(plan, nextConfig);
};
const runReload = async () => {
if (stopped) return;
if (running) {
pending = true;
return;
}
running = true;
if (debounceTimer) {
clearTimeout(debounceTimer);
debounceTimer = null;
}
try {
const snapshot = await opts.readSnapshot();
if (handleMissingSnapshot(snapshot)) return;
if (handleInvalidSnapshot(snapshot)) return;
await applySnapshot(snapshot.config);
} catch (err) {
opts.log.error(`config reload failed: ${String(err)}`);
} finally {
running = false;
if (pending) {
pending = false;
schedule();
}
}
};
const watcher = chokidar.watch(opts.watchPath, {
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: 200,
pollInterval: 50
},
usePolling: Boolean(process.env.VITEST)
});
watcher.on("add", schedule);
watcher.on("change", schedule);
watcher.on("unlink", schedule);
let watcherClosed = false;
watcher.on("error", (err) => {
if (watcherClosed) return;
watcherClosed = true;
opts.log.warn(`config watcher error: ${String(err)}`);
watcher.close().catch(() => {});
});
return { stop: async () => {
stopped = true;
if (debounceTimer) clearTimeout(debounceTimer);
debounceTimer = null;
watcherClosed = true;
await watcher.close().catch(() => {});
} };
}
//#endregion
//#region src/gateway/events.ts
const GATEWAY_EVENT_UPDATE_AVAILABLE = "update.available";
//#endregion
//#region src/gateway/exec-approval-manager.ts
const RESOLVED_ENTRY_GRACE_MS = 15e3;
var ExecApprovalManager = class {
constructor() {
this.pending = /* @__PURE__ */ new Map();
}
create(request, timeoutMs, id) {
const now = Date.now();
return {
id: id && id.trim().length > 0 ? id.trim() : randomUUID(),
request,
createdAtMs: now,
expiresAtMs: now + timeoutMs
};
}
/**
* Register an approval record and return a promise that resolves when the decision is made.
* This separates registration (synchronous) from waiting (async), allowing callers to
* confirm registration before the decision is made.
*/
register(record, timeoutMs) {
const existing = this.pending.get(record.id);
if (existing) {
if (existing.record.resolvedAtMs === void 0) return existing.promise;
throw new Error(`approval id '${record.id}' already resolved`);
}
let resolvePromise;
let rejectPromise;
const promise = new Promise((resolve, reject) => {
resolvePromise = resolve;
rejectPromise = reject;
});
const entry = {
record,
resolve: resolvePromise,
reject: rejectPromise,
timer: null,
promise
};
entry.timer = setTimeout(() => {
this.expire(record.id);
}, timeoutMs);
this.pending.set(record.id, entry);
return promise;
}
/**
* @deprecated Use register() instead for explicit separation of registration and waiting.
*/
async waitForDecision(record, timeoutMs) {
return this.register(record, timeoutMs);
}
resolve(recordId, decision, resolvedBy) {
const pending = this.pending.get(recordId);
if (!pending) return false;
if (pending.record.resolvedAtMs !== void 0) return false;
clearTimeout(pending.timer);
pending.record.resolvedAtMs = Date.now();
pending.record.decision = decision;
pending.record.resolvedBy = resolvedBy ?? null;
pending.resolve(decision);
setTimeout(() => {
if (this.pending.get(recordId) === pending) this.pending.delete(recordId);
}, RESOLVED_ENTRY_GRACE_MS);
return true;
}
expire(recordId, resolvedBy) {
const pending = this.pending.get(recordId);
if (!pending) return false;
if (pending.record.resolvedAtMs !== void 0) return false;
clearTimeout(pending.timer);
pending.record.resolvedAtMs = Date.now();
pending.record.decision = void 0;
pending.record.resolvedBy = resolvedBy ?? null;
pending.resolve(null);
setTimeout(() => {
if (this.pending.get(recordId) === pending) this.pending.delete(recordId);
}, RESOLVED_ENTRY_GRACE_MS);
return true;
}
getSnapshot(recordId) {
return this.pending.get(recordId)?.record ?? null;
}
consumeAllowOnce(recordId) {
const entry = this.pending.get(recordId);
if (!entry) return false;
const record = entry.record;
if (record.decision !== "allow-once") return false;
record.decision = void 0;
return true;
}
/**
* Wait for decision on an already-registered approval.
* Returns the decision promise if the ID is pending, null otherwise.
*/
awaitDecision(recordId) {
return this.pending.get(recordId)?.promise ?? null;
}
lookupPendingId(input) {
const normalized = input.trim();
if (!normalized) return { kind: "none" };
const exact = this.pending.get(normalized);
if (exact) return exact.record.resolvedAtMs === void 0 ? {
kind: "exact",
id: normalized
} : { kind: "none" };
const lowerPrefix = normalized.toLowerCase();
const matches = [];
for (const [id, entry] of this.pending.entries()) {
if (entry.record.resolvedAtMs !== void 0) continue;
if (id.toLowerCase().startsWith(lowerPrefix)) matches.push(id);
}
if (matches.length === 1) return {
kind: "prefix",
id: matches[0]
};
if (matches.length > 1) return {
kind: "ambiguous",
ids: matches
};
return { kind: "none" };
}
};
//#endregion
//#region src/gateway/node-registry.ts
var NodeRegistry = class {
constructor() {
this.nodesById = /* @__PURE__ */ new Map();
this.nodesByConn = /* @__PURE__ */ new Map();
this.pendingInvokes = /* @__PURE__ */ new Map();
}
register(client, opts) {
const connect = client.connect;
const nodeId = connect.device?.id ?? connect.client.id;
const caps = Array.isArray(connect.caps) ? connect.caps : [];
const commands = Array.isArray(connect.commands) ? connect.commands ?? [] : [];
const permissions = typeof connect.permissions === "object" ? connect.permissions ?? void 0 : void 0;
const pathEnv = typeof connect.pathEnv === "string" ? connect.pathEnv : void 0;
const session = {
nodeId,
connId: client.connId,
client,
displayName: connect.client.displayName,
platform: connect.client.platform,
version: connect.client.version,
coreVersion: connect.coreVersion,
uiVersion: connect.uiVersion,
deviceFamily: connect.client.deviceFamily,
modelIdentifier: connect.client.modelIdentifier,
remoteIp: opts.remoteIp,
caps,
commands,
permissions,
pathEnv,
connectedAtMs: Date.now()
};
this.nodesById.set(nodeId, session);
this.nodesByConn.set(client.connId, nodeId);
return session;
}
unregister(connId) {
const nodeId = this.nodesByConn.get(connId);
if (!nodeId) return null;
this.nodesByConn.delete(connId);
this.nodesById.delete(nodeId);
for (const [id, pending] of this.pendingInvokes.entries()) {
if (pending.nodeId !== nodeId) continue;
clearTimeout(pending.timer);
pending.reject(/* @__PURE__ */ new Error(`node disconnected (${pending.command})`));
this.pendingInvokes.delete(id);
}
return nodeId;
}
listConnected() {
return [...this.nodesById.values()];
}
get(nodeId) {
return this.nodesById.get(nodeId);
}
async invoke(params) {
const node = this.nodesById.get(params.nodeId);
if (!node) return {
ok: false,
error: {
code: "NOT_CONNECTED",
message: "node not connected"
}
};
const requestId = randomUUID();
const payload = {
id: requestId,
nodeId: params.nodeId,
command: params.command,
paramsJSON: "params" in params && params.params !== void 0 ? JSON.stringify(params.params) : null,
timeoutMs: params.timeoutMs,
idempotencyKey: params.idempotencyKey
};
if (!this.sendEventToSession(node, "node.invoke.request", payload)) return {
ok: false,
error: {
code: "UNAVAILABLE",
message: "failed to send invoke to node"
}
};
const timeoutMs = typeof params.timeoutMs === "number" ? params.timeoutMs : 3e4;
return await new Promise((resolve, reject) => {
const timer = setTimeout(() => {
this.pendingInvokes.delete(requestId);
resolve({
ok: false,
error: {
code: "TIMEOUT",
message: "node invoke timed out"
}
});
}, timeoutMs);
this.pendingInvokes.set(requestId, {
nodeId: params.nodeId,
command: params.command,
resolve,
reject,
timer
});
});
}
handleInvokeResult(params) {
const pending = this.pendingInvokes.get(params.id);
if (!pending) return false;
if (pending.nodeId !== params.nodeId) return false;
clearTimeout(pending.timer);
this.pendingInvokes.delete(params.id);
pending.resolve({
ok: params.ok,
payload: params.payload,
payloadJSON: params.payloadJSON ?? null,
error: params.error ?? null
});
return true;
}
sendEvent(nodeId, event, payload) {
const node = this.nodesById.get(nodeId);
if (!node) return false;
return this.sendEventToSession(node, event, payload);
}
sendEventInternal(node, event, payload) {
try {
node.client.socket.send(JSON.stringify({
type: "event",
event,
payload
}));
return true;
} catch {
return false;
}
}
sendEventToSession(node, event, payload) {
return this.sendEventInternal(node, event, payload);
}
};
//#endregion
//#region src/gateway/server-channels.ts
const CHANNEL_RESTART_POLICY = {
initialMs: 5e3,
maxMs: 5 * 6e4,
factor: 2,
jitter: .1
};
const MAX_RESTART_ATTEMPTS = 10;
function createRuntimeStore() {
return {
aborts: /* @__PURE__ */ new Map(),
starting: /* @__PURE__ */ new Map(),
tasks: /* @__PURE__ */ new Map(),
runtimes: /* @__PURE__ */ new Map()
};
}
function isAccountEnabled(account) {
if (!account || typeof account !== "object") return true;
return account.enabled !== false;
}
function resolveDefaultRuntime(channelId) {
return getChannelPlugin(channelId)?.status?.defaultRuntime ?? { accountId: "default" };
}
function cloneDefaultRuntime(channelId, accountId) {
return {
...resolveDefaultRuntime(channelId),
accountId
};
}
function createChannelManager(opts) {
const { loadConfig, channelLogs, channelRuntimeEnvs, channelRuntime, resolveChannelRuntime } = opts;
const channelStores = /* @__PURE__ */ new Map();
const restartAttempts = /* @__PURE__ */ new Map();
const manuallyStopped = /* @__PURE__ */ new Set();
const restartKey = (channelId, accountId) => `${channelId}:${accountId}`;
const resolveAccountHealthMonitorOverride = (channelConfig, accountId) => {
if (!channelConfig?.accounts) return;
const direct = resolveAccountEntry(channelConfig.accounts, accountId);
if (typeof direct?.healthMonitor?.enabled === "boolean") return direct.healthMonitor.enabled;
const normalizedAccountId = normalizeOptionalAccountId(accountId);
if (!normalizedAccountId) return;
const match = resolveNormalizedAccountEntry(channelConfig.accounts, normalizedAccountId, normalizeAccountId$1);
if (typeof match?.healthMonitor?.enabled !== "boolean") return;
return match.healthMonitor.enabled;
};
const isHealthMonitorEnabled = (channelId, accountId) => {
const cfg = loadConfig();
const channelConfig = cfg.channels?.[channelId];
const accountOverride = resolveAccountHealthMonitorOverride(channelConfig, accountId);
const channelOverride = channelConfig?.healthMonitor?.enabled;
if (typeof accountOverride === "boolean") return accountOverride;
if (typeof channelOverride === "boolean") return channelOverride;
const plugin = getChannelPlugin(channelId);
if (!plugin) return true;
try {
plugin.config.resolveAccount(cfg, accountId);
} catch (err) {
channelLogs[channelId].warn?.(`[${channelId}:${accountId}] health-monitor: failed to resolve account; skipping monitor (${formatErrorMessage(err)})`);
return false;
}
return true;
};
const getStore = (channelId) => {
const existing = channelStores.get(channelId);
if (existing) return existing;
const next = createRuntimeStore();
channelStores.set(channelId, next);
return next;
};
const getRuntime = (channelId, accountId) => {
return getStore(channelId).runtimes.get(accountId) ?? cloneDefaultRuntime(channelId, accountId);
};
const setRuntime = (channelId, accountId, patch) => {
const store = getStore(channelId);
const next = {
...getRuntime(channelId, accountId),
...patch,
accountId
};
store.runtimes.set(accountId, next);
return next;
};
const getChannelRuntime = () => {
return channelRuntime ?? resolveChannelRuntime?.();
};
const startChannelInternal = async (channelId, accountId, opts = {}) => {
const plugin = getChannelPlugin(channelId);
const startAccount = plugin?.gateway?.startAccount;
if (!startAccount) return;
const { preserveRestartAttempts = false, preserveManualStop = false } = opts;
const cfg = loadConfig();
resetDirectoryCache({
channel: channelId,
accountId
});
const store = getStore(channelId);
const accountIds = accountId ? [accountId] : plugin.config.listAccountIds(cfg);
if (accountIds.length === 0) return;
await Promise.all(accountIds.map(async (id) => {
if (store.tasks.has(id)) return;
const existingStart = store.starting.get(id);
if (existingStart) {
await existingStart;
return;
}
let resolveStart;
const startGate = new Promise((resolve) => {
resolveStart = resolve;
});
store.starting.set(id, startGate);
const abort = new AbortController();
store.aborts.set(id, abort);
let handedOffTask = false;
try {
const account = plugin.config.resolveAccount(cfg, id);
if (!(plugin.config.isEnabled ? plugin.config.isEnabled(account, cfg) : isAccountEnabled(account))) {
setRuntime(channelId, id, {
accountId: id,
enabled: false,
configured: true,
running: false,
restartPending: false,
lastError: plugin.config.disabledReason?.(account, cfg) ?? "disabled"
});
return;
}
let configured = true;
if (plugin.config.isConfigured) configured = await plugin.config.isConfigured(account, cfg);
if (!configured) {
setRuntime(channelId, id, {
accountId: id,
enabled: true,
configured: false,
running: false,
restartPending: false,
lastError: plugin.config.unconfiguredReason?.(account, cfg) ?? "not configured"
});
return;
}
const rKey = restartKey(channelId, id);
if (!preserveManualStop) manuallyStopped.delete(rKey);
if (abort.signal.aborted || manuallyStopped.has(rKey)) {
setRuntime(channelId, id, {
accountId: id,
running: false,
restartPending: false,
lastStopAt: Date.now()
});
return;
}
if (!preserveRestartAttempts) restartAttempts.delete(rKey);
setRuntime(channelId, id, {
accountId: id,
enabled: true,
configured: true,
running: true,
restartPending: false,
lastStartAt: Date.now(),
lastError: null,
reconnectAttempts: preserveRestartAttempts ? restartAttempts.get(rKey) ?? 0 : 0
});
const log = channelLogs[channelId];
const resolvedChannelRuntime = getChannelRuntime();
const task = startAccount({
cfg,
accountId: id,
account,
runtime: channelRuntimeEnvs[channelId],
abortSignal: abort.signal,
log,
getStatus: () => getRuntime(channelId, id),
setStatus: (next) => setRuntime(channelId, id, next),
...resolvedChannelRuntime ? { channelRuntime: resolvedChannelRuntime } : {}
});
const trackedPromise = Promise.resolve(task).catch((err) => {
const message = formatErrorMessage(err);
setRuntime(channelId, id, {
accountId: id,
lastError: message
});
log.error?.(`[${id}] channel exited: ${message}`);
}).finally(() => {
setRuntime(channelId, id, {
accountId: id,
running: false,
lastStopAt: Date.now()
});
}).then(async () => {
if (manuallyStopped.has(rKey)) return;
const attempt = (restartAttempts.get(rKey) ?? 0) + 1;
restartAttempts.set(rKey, attempt);
if (attempt > MAX_RESTART_ATTEMPTS) {
setRuntime(channelId, id, {
accountId: id,
restartPending: false,
reconnectAttempts: attempt
});
log.error?.(`[${id}] giving up after ${MAX_RESTART_ATTEMPTS} restart attempts`);
return;
}
const delayMs = computeBackoff(CHANNEL_RESTART_POLICY, attempt);
log.info?.(`[${id}] auto-restart attempt ${attempt}/${MAX_RESTART_ATTEMPTS} in ${Math.round(delayMs / 1e3)}s`);
setRuntime(channelId, id, {
accountId: id,
restartPending: true,
reconnectAttempts: attempt
});
try {
await sleepWithAbort(delayMs, abort.signal);
if (manuallyStopped.has(rKey)) return;
if (store.tasks.get(id) === trackedPromise) store.tasks.delete(id);
if (store.aborts.get(id) === abort) store.aborts.delete(id);
await startChannelInternal(channelId, id, {
preserveRestartAttempts: true,
preserveManualStop: true
});
} catch {}
}).finally(() => {
if (store.tasks.get(id) === trackedPromise) store.tasks.delete(id);
if (store.aborts.get(id) === abort) store.aborts.delete(id);
});
handedOffTask = true;
store.tasks.set(id, trackedPromise);
} finally {
resolveStart?.();
if (store.starting.get(id) === startGate) store.starting.delete(id);
if (!handedOffTask && store.aborts.get(id) === abort) store.aborts.delete(id);
}
}));
};
const startChannel = async (channelId, accountId) => {
await startChannelInternal(channelId, accountId);
};
const stopChannel = async (channelId, accountId) => {
const plugin = getChannelPlugin(channelId);
const store = getStore(channelId);
if (!plugin?.gateway?.stopAccount && store.aborts.size === 0 && store.tasks.size === 0) return;
const cfg = loadConfig();
const knownIds = new Set([
...store.aborts.keys(),
...store.starting.keys(),
...store.tasks.keys(),
...plugin ? plugin.config.listAccountIds(cfg) : []
]);
if (accountId) {
knownIds.clear();
knownIds.add(accountId);
}
await Promise.all(Array.from(knownIds.values()).map(async (id) => {
const abort = store.aborts.get(id);
const task = store.tasks.get(id);
if (!abort && !task && !plugin?.gateway?.stopAccount) return;
manuallyStopped.add(restartKey(channelId, id));
abort?.abort();
if (plugin?.gateway?.stopAccount) {
const account = plugin.config.resolveAccount(cfg, id);
await plugin.gateway.stopAccount({
cfg,
accountId: id,
account,
runtime: channelRuntimeEnvs[channelId],
abortSignal: abort?.signal ?? new AbortController().signal,
log: channelLogs[channelId],
getStatus: () => getRuntime(channelId, id),
setStatus: (next) => setRuntime(channelId, id, next)
});
}
try {
await task;
} catch {}
store.aborts.delete(id);
store.tasks.delete(id);
setRuntime(channelId, id, {
accountId: id,
running: false,
restartPending: false,
lastStopAt: Date.now()
});
}));
};
const startChannels = async () => {
for (const plugin of listChannelPlugins()) await startChannel(plugin.id);
};
const markChannelLoggedOut = (channelId, cleared, accountId) => {
const plugin = getChannelPlugin(channelId);
if (!plugin) return;
const cfg = loadConfig();
const resolvedId = accountId ?? resolveChannelDefaultAccountId({
plugin,
cfg
});
const current = getRuntime(channelId, resolvedId);
const next = {
accountId: resolvedId,
running: false,
restartPending: false,
lastError: cleared ? "logged out" : current.lastError
};
if (typeof current.connected === "boolean") next.connected = false;
setRuntime(channelId, resolvedId, next);
};
const getRuntimeSnapshot = () => {
const cfg = loadConfig();
const channels = {};
const channelAccounts = {};
for (const plugin of listChannelPlugins()) {
const store = getStore(plugin.id);
const accountIds = plugin.config.listAccountIds(cfg);
const defaultAccountId = resolveChannelDefaultAccountId({
plugin,
cfg,
accountIds
});
const accounts = {};
for (const id of accountIds) {
const account = plugin.config.resolveAccount(cfg, id);
const enabled = plugin.config.isEnabled ? plugin.config.isEnabled(account, cfg) : isAccountEnabled(account);
const configured = (plugin.config.describeAccount?.(account, cfg))?.configured;
const next = {
...store.runtimes.get(id) ?? cloneDefaultRuntime(plugin.id, id),
accountId: id
};
next.enabled = enabled;
next.configured = typeof configured === "boolean" ? configured : next.configured ?? true;
if (!next.running) {
if (!enabled) next.lastError ??= plugin.config.disabledReason?.(account, cfg) ?? "disabled";
else if (configured === false) next.lastError ??= plugin.config.unconfiguredReason?.(account, cfg) ?? "not configured";
}
accounts[id] = next;
}
const defaultAccount = accounts[defaultAccountId] ?? cloneDefaultRuntime(plugin.id, defaultAccountId);
channels[plugin.id] = defaultAccount;
channelAccounts[plugin.id] = accounts;
}
return {
channels,
channelAccounts
};
};
const isManuallyStopped_ = (channelId, accountId) => {
return manuallyStopped.has(restartKey(channelId, accountId));
};
const resetRestartAttempts_ = (channelId, accountId) => {
restartAttempts.delete(restartKey(channelId, accountId));
};
return {
getRuntimeSnapshot,
startChannels,
startChannel,
stopChannel,
markChannelLoggedOut,
isManuallyStopped: isManuallyStopped_,
resetRestartAttempts: resetRestartAttempts_,
isHealthMonitorEnabled
};
}
//#endregion
//#region src/gateway/session-lifecycle-state.ts
function isFiniteTimestamp$1(value) {
return typeof value === "number" && Number.isFinite(value) && value > 0;
}
function resolveLifecyclePhase(event) {
const phase = typeof event.data?.phase === "string" ? event.data.phase : "";
return phase === "start" || phase === "end" || phase === "error" ? phase : null;
}
function resolveTerminalStatus(event) {
if (resolveLifecyclePhase(event) === "error") return "failed";
if ((typeof event.data?.stopReason === "string" ? event.data.stopReason : "") === "aborted") return "killed";
return event.data?.aborted === true ? "timeout" : "done";
}
function resolveLifecycleStartedAt(existingStartedAt, event) {
if (isFiniteTimestamp$1(event.data?.startedAt)) return event.data.startedAt;
if (isFiniteTimestamp$1(existingStartedAt)) return existingStartedAt;
return isFiniteTimestamp$1(event.ts) ? event.ts : void 0;
}
function resolveLifecycleEndedAt(event) {
if (isFiniteTimestamp$1(event.data?.endedAt)) return event.data.endedAt;
return isFiniteTimestamp$1(event.ts) ? event.ts : void 0;
}
function resolveRuntimeMs(params) {
const { startedAt, endedAt, existingRuntimeMs } = params;
if (isFiniteTimestamp$1(startedAt) && isFiniteTimestamp$1(endedAt)) return Math.max(0, endedAt - startedAt);
if (typeof existingRuntimeMs === "number" && Number.isFinite(existingRuntimeMs) && existingRuntimeMs >= 0) return existingRuntimeMs;
}
function deriveGatewaySessionLifecycleSnapshot(params) {
const phase = resolveLifecyclePhase(params.event);
if (!phase) return {};
const existing = params.session ?? void 0;
if (phase === "start") {
const startedAt = resolveLifecycleStartedAt(existing?.startedAt, params.event);
return {
updatedAt: startedAt ?? existing?.updatedAt,
status: "running",
startedAt,
endedAt: void 0,
runtimeMs: void 0,
abortedLastRun: false
};
}
const startedAt = resolveLifecycleStartedAt(existing?.startedAt, params.event);
const endedAt = resolveLifecycleEndedAt(params.event);
return {
updatedAt: endedAt ?? existing?.updatedAt,
status: resolveTerminalStatus(params.event),
startedAt,
endedAt,
runtimeMs: resolveRuntimeMs({
startedAt,
endedAt,
existingRuntimeMs: existing?.runtimeMs
}),
abortedLastRun: resolveTerminalStatus(params.event) === "killed"
};
}
function derivePersistedSessionLifecyclePatch(params) {
const snapshot = deriveGatewaySessionLifecycleSnapshot({
session: params.entry ?? void 0,
event: params.event
});
return {
...snapshot,
updatedAt: typeof snapshot.updatedAt === "number" ? snapshot.updatedAt : void 0
};
}
async function persistGatewaySessionLifecycleEvent(params) {
if (!resolveLifecyclePhase(params.event)) return;
const sessionEntry = loadSessionEntry(params.sessionKey);
if (!sessionEntry.entry) return;
await updateSessionStoreEntry({
storePath: sessionEntry.storePath,
sessionKey: sessionEntry.canonicalKey,
update: async (entry) => derivePersistedSessionLifecyclePatch({
entry,
event: params.event
})
});
}
//#endregion
//#region src/gateway/server-chat.ts
function resolveHeartbeatAckMaxChars$1() {
try {
const cfg = loadConfig();
return Math.max(0, cfg.agents?.defaults?.heartbeat?.ackMaxChars ?? 300);
} catch {
return 300;
}
}
function resolveHeartbeatContext(runId, sourceRunId) {
const primary = getAgentRunContext(runId);
if (primary?.isHeartbeat) return primary;
if (sourceRunId && sourceRunId !== runId) {
const source = getAgentRunContext(sourceRunId);
if (source?.isHeartbeat) return source;
}
return primary;
}
/**
* Check if heartbeat ACK/noise should be hidden from interactive chat surfaces.
*/
function shouldHideHeartbeatChatOutput(runId, sourceRunId) {
if (!resolveHeartbeatContext(runId, sourceRunId)?.isHeartbeat) return false;
try {
return !resolveHeartbeatVisibility({
cfg: loadConfig(),
channel: "webchat"
}).showOk;
} catch {
return true;
}
}
function normalizeHeartbeatChatFinalText(params) {
if (!shouldHideHeartbeatChatOutput(params.runId, params.sourceRunId)) return {
suppress: false,
text: params.text
};
const stripped = stripHeartbeatToken(params.text, {
mode: "heartbeat",
maxAckChars: resolveHeartbeatAckMaxChars$1()
});
if (!stripped.didStrip) return {
suppress: false,
text: params.text
};
if (stripped.shouldSkip) return {
suppress: true,
text: ""
};
return {
suppress: false,
text: stripped.text
};
}
function isSilentReplyLeadFragment(text) {
const normalized = text.trim().toUpperCase();
if (!normalized) return false;
if (!/^[A-Z_]+$/.test(normalized)) return false;
if (normalized === "NO_REPLY") return false;
return SILENT_REPLY_TOKEN.startsWith(normalized);
}
function appendUniqueSuffix(base, suffix) {
if (!suffix) return base;
if (!base) return suffix;
if (base.endsWith(suffix)) return base;
const maxOverlap = Math.min(base.length, suffix.length);
for (let overlap = maxOverlap; overlap > 0; overlap -= 1) if (base.slice(-overlap) === suffix.slice(0, overlap)) return base + suffix.slice(overlap);
return base + suffix;
}
function resolveMergedAssistantText(params) {
const { previousText, nextText, nextDelta } = params;
if (nextText && previousText) {
if (nextText.startsWith(previousText)) return nextText;
if (previousText.startsWith(nextText) && !nextDelta) return previousText;
}
if (nextDelta) return appendUniqueSuffix(previousText, nextDelta);
if (nextText) return nextText;
return previousText;
}
function createChatRunRegistry() {
const chatRunSessions = /* @__PURE__ */ new Map();
const add = (sessionId, entry) => {
const queue = chatRunSessions.get(sessionId);
if (queue) queue.push(entry);
else chatRunSessions.set(sessionId, [entry]);
};
const peek = (sessionId) => chatRunSessions.get(sessionId)?.[0];
const shift = (sessionId) => {
const queue = chatRunSessions.get(sessionId);
if (!queue || queue.length === 0) return;
const entry = queue.shift();
if (!queue.length) chatRunSessions.delete(sessionId);
return entry;
};
const remove = (sessionId, clientRunId, sessionKey) => {
const queue = chatRunSessions.get(sessionId);
if (!queue || queue.length === 0) return;
const idx = queue.findIndex((entry) => entry.clientRunId === clientRunId && (sessionKey ? entry.sessionKey === sessionKey : true));
if (idx < 0) return;
const [entry] = queue.splice(idx, 1);
if (!queue.length) chatRunSessions.delete(sessionId);
return entry;
};
const clear = () => {
chatRunSessions.clear();
};
return {
add,
peek,
shift,
remove,
clear
};
}
function createChatRunState() {
const registry = createChatRunRegistry();
const buffers = /* @__PURE__ */ new Map();
const deltaSentAt = /* @__PURE__ */ new Map();
const deltaLastBroadcastLen = /* @__PURE__ */ new Map();
const abortedRuns = /* @__PURE__ */ new Map();
const clear = () => {
registry.clear();
buffers.clear();
deltaSentAt.clear();
deltaLastBroadcastLen.clear();
abortedRuns.clear();
};
return {
registry,
buffers,
deltaSentAt,
deltaLastBroadcastLen,
abortedRuns,
clear
};
}
const TOOL_EVENT_RECIPIENT_TTL_MS = 600 * 1e3;
const TOOL_EVENT_RECIPIENT_FINAL_GRACE_MS = 30 * 1e3;
function createSessionEventSubscriberRegistry() {
const connIds = /* @__PURE__ */ new Set();
const empty = /* @__PURE__ */ new Set();
return {
subscribe: (connId) => {
const normalized = connId.trim();
if (!normalized) return;
connIds.add(normalized);
},
unsubscribe: (connId) => {
const normalized = connId.trim();
if (!normalized) return;
connIds.delete(normalized);
},
getAll: () => connIds.size > 0 ? connIds : empty,
clear: () => {
connIds.clear();
}
};
}
function createSessionMessageSubscriberRegistry() {
const sessionToConnIds = /* @__PURE__ */ new Map();
const connToSessionKeys = /* @__PURE__ */ new Map();
const empty = /* @__PURE__ */ new Set();
const normalize = (value) => value.trim();
return {
subscribe: (connId, sessionKey) => {
const normalizedConnId = normalize(connId);
const normalizedSessionKey = normalize(sessionKey);
if (!normalizedConnId || !normalizedSessionKey) return;
const connIds = sessionToConnIds.get(normalizedSessionKey) ?? /* @__PURE__ */ new Set();
connIds.add(normalizedConnId);
sessionToConnIds.set(normalizedSessionKey, connIds);
const sessionKeys = connToSessionKeys.get(normalizedConnId) ?? /* @__PURE__ */ new Set();
sessionKeys.add(normalizedSessionKey);
connToSessionKeys.set(normalizedConnId, sessionKeys);
},
unsubscribe: (connId, sessionKey) => {
const normalizedConnId = normalize(connId);
const normalizedSessionKey = normalize(sessionKey);
if (!normalizedConnId || !normalizedSessionKey) return;
const connIds = sessionToConnIds.get(normalizedSessionKey);
if (connIds) {
connIds.delete(normalizedConnId);
if (connIds.size === 0) sessionToConnIds.delete(normalizedSessionKey);
}
const sessionKeys = connToSessionKeys.get(normalizedConnId);
if (sessionKeys) {
sessionKeys.delete(normalizedSessionKey);
if (sessionKeys.size === 0) connToSessionKeys.delete(normalizedConnId);
}
},
unsubscribeAll: (connId) => {
const normalizedConnId = normalize(connId);
if (!normalizedConnId) return;
const sessionKeys = connToSessionKeys.get(normalizedConnId);
if (!sessionKeys) return;
for (const sessionKey of sessionKeys) {
const connIds = sessionToConnIds.get(sessionKey);
if (!connIds) continue;
connIds.delete(normalizedConnId);
if (connIds.size === 0) sessionToConnIds.delete(sessionKey);
}
connToSessionKeys.delete(normalizedConnId);
},
get: (sessionKey) => {
const normalizedSessionKey = normalize(sessionKey);
if (!normalizedSessionKey) return empty;
return sessionToConnIds.get(normalizedSessionKey) ?? empty;
},
clear: () => {
sessionToConnIds.clear();
connToSessionKeys.clear();
}
};
}
function createToolEventRecipientRegistry() {
const recipients = /* @__PURE__ */ new Map();
const prune = () => {
if (recipients.size === 0) return;
const now = Date.now();
for (const [runId, entry] of recipients) if (now >= (entry.finalizedAt ? entry.finalizedAt + TOOL_EVENT_RECIPIENT_FINAL_GRACE_MS : entry.updatedAt + TOOL_EVENT_RECIPIENT_TTL_MS)) recipients.delete(runId);
};
const add = (runId, connId) => {
if (!runId || !connId) return;
const now = Date.now();
const existing = recipients.get(runId);
if (existing) {
existing.connIds.add(connId);
existing.updatedAt = now;
} else recipients.set(runId, {
connIds: new Set([connId]),
updatedAt: now
});
prune();
};
const get = (runId) => {
const entry = recipients.get(runId);
if (!entry) return;
entry.updatedAt = Date.now();
prune();
return entry.connIds;
};
const markFinal = (runId) => {
const entry = recipients.get(runId);
if (!entry) return;
entry.finalizedAt = Date.now();
prune();
};
return {
add,
get,
markFinal
};
}
function createAgentEventHandler({ broadcast, broadcastToConnIds, nodeSendToSession, agentRunSeq, chatRunState, resolveSessionKeyForRun, clearAgentRunContext, toolEventRecipients, sessionEventSubscribers }) {
const buildSessionEventSnapshot = (sessionKey, evt) => {
const row = loadGatewaySessionRow(sessionKey);
const lifecyclePatch = evt ? deriveGatewaySessionLifecycleSnapshot({
session: row ? {
updatedAt: row.updatedAt ?? void 0,
status: row.status,
startedAt: row.startedAt,
endedAt: row.endedAt,
runtimeMs: row.runtimeMs,
abortedLastRun: row.abortedLastRun
} : void 0,
event: evt
}) : {};
const session = row ? {
...row,
...lifecyclePatch
} : void 0;
const snapshotSource = session ?? lifecyclePatch;
return {
...session ? { session } : {},
totalTokens: row?.totalTokens,
totalTokensFresh: row?.totalTokensFresh,
contextTokens: row?.contextTokens,
estimatedCostUsd: row?.estimatedCostUsd,
modelProvider: row?.modelProvider,
model: row?.model,
status: snapshotSource.status,
startedAt: snapshotSource.startedAt,
endedAt: snapshotSource.endedAt,
runtimeMs: snapshotSource.runtimeMs,
updatedAt: snapshotSource.updatedAt,
abortedLastRun: snapshotSource.abortedLastRun
};
};
const emitChatDelta = (sessionKey, clientRunId, sourceRunId, seq, text, delta) => {
const cleanedText = stripInlineDirectiveTagsForDisplay(text).text;
const cleanedDelta = typeof delta === "string" ? stripInlineDirectiveTagsForDisplay(delta).text : "";
const mergedText = resolveMergedAssistantText({
previousText: chatRunState.buffers.get(clientRunId) ?? "",
nextText: cleanedText,
nextDelta: cleanedDelta
});
if (!mergedText) return;
chatRunState.buffers.set(clientRunId, mergedText);
if (isSilentReplyText(mergedText, "NO_REPLY")) return;
if (isSilentReplyLeadFragment(mergedText)) return;
if (shouldHideHeartbeatChatOutput(clientRunId, sourceRunId)) return;
const now = Date.now();
if (now - (chatRunState.deltaSentAt.get(clientRunId) ?? 0) < 150) return;
chatRunState.deltaSentAt.set(clientRunId, now);
chatRunState.deltaLastBroadcastLen.set(clientRunId, mergedText.length);
const payload = {
runId: clientRunId,
sessionKey,
seq,
state: "delta",
message: {
role: "assistant",
content: [{
type: "text",
text: mergedText
}],
timestamp: now
}
};
broadcast("chat", payload, { dropIfSlow: true });
nodeSendToSession(sessionKey, "chat", payload);
};
const resolveBufferedChatTextState = (clientRunId, sourceRunId) => {
const normalizedHeartbeatText = normalizeHeartbeatChatFinalText({
runId: clientRunId,
sourceRunId,
text: stripInlineDirectiveTagsForDisplay(chatRunState.buffers.get(clientRunId) ?? "").text.trim()
});
const text = normalizedHeartbeatText.text.trim();
return {
text,
shouldSuppressSilent: normalizedHeartbeatText.suppress || isSilentReplyText(text, "NO_REPLY")
};
};
const flushBufferedChatDeltaIfNeeded = (sessionKey, clientRunId, sourceRunId, seq) => {
const { text, shouldSuppressSilent } = resolveBufferedChatTextState(clientRunId, sourceRunId);
const shouldSuppressSilentLeadFragment = isSilentReplyLeadFragment(text);
const shouldSuppressHeartbeatStreaming = shouldHideHeartbeatChatOutput(clientRunId, sourceRunId);
if (!text || shouldSuppressSilent || shouldSuppressSilentLeadFragment || shouldSuppressHeartbeatStreaming) return;
const lastBroadcastLen = chatRunState.deltaLastBroadcastLen.get(clientRunId) ?? 0;
if (text.length <= lastBroadcastLen) return;
const now = Date.now();
const flushPayload = {
runId: clientRunId,
sessionKey,
seq,
state: "delta",
message: {
role: "assistant",
content: [{
type: "text",
text
}],
timestamp: now
}
};
broadcast("chat", flushPayload, { dropIfSlow: true });
nodeSendToSession(sessionKey, "chat", flushPayload);
chatRunState.deltaLastBroadcastLen.set(clientRunId, text.length);
chatRunState.deltaSentAt.set(clientRunId, now);
};
const emitChatFinal = (sessionKey, clientRunId, sourceRunId, seq, jobState, error, stopReason) => {
const { text, shouldSuppressSilent } = resolveBufferedChatTextState(clientRunId, sourceRunId);
flushBufferedChatDeltaIfNeeded(sessionKey, clientRunId, sourceRunId, seq);
chatRunState.deltaLastBroadcastLen.delete(clientRunId);
chatRunState.buffers.delete(clientRunId);
chatRunState.deltaSentAt.delete(clientRunId);
if (jobState === "done") {
const payload = {
runId: clientRunId,
sessionKey,
seq,
state: "final",
...stopReason && { stopReason },
message: text && !shouldSuppressSilent ? {
role: "assistant",
content: [{
type: "text",
text
}],
timestamp: Date.now()
} : void 0
};
broadcast("chat", payload);
nodeSendToSession(sessionKey, "chat", payload);
return;
}
const payload = {
runId: clientRunId,
sessionKey,
seq,
state: "error",
errorMessage: error ? formatForLog(error) : void 0
};
broadcast("chat", payload);
nodeSendToSession(sessionKey, "chat", payload);
};
const resolveToolVerboseLevel = (runId, sessionKey) => {
const runVerbose = normalizeVerboseLevel(getAgentRunContext(runId)?.verboseLevel);
if (runVerbose) return runVerbose;
if (!sessionKey) return "off";
try {
const { cfg, entry } = loadSessionEntry(sessionKey);
const sessionVerbose = normalizeVerboseLevel(entry?.verboseLevel);
if (sessionVerbose) return sessionVerbose;
return normalizeVerboseLevel(cfg.agents?.defaults?.verboseDefault) ?? "off";
} catch {
return "off";
}
};
return (evt) => {
const chatLink = chatRunState.registry.peek(evt.runId);
const eventSessionKey = typeof evt.sessionKey === "string" && evt.sessionKey.trim() ? evt.sessionKey : void 0;
const isControlUiVisible = getAgentRunContext(evt.runId)?.isControlUiVisible ?? true;
const sessionKey = chatLink?.sessionKey ?? eventSessionKey ?? resolveSessionKeyForRun(evt.runId);
const clientRunId = chatLink?.clientRunId ?? evt.runId;
const eventRunId = chatLink?.clientRunId ?? evt.runId;
const eventForClients = chatLink ? {
...evt,
runId: eventRunId
} : evt;
const isAborted = chatRunState.abortedRuns.has(clientRunId) || chatRunState.abortedRuns.has(evt.runId);
const agentPayload = sessionKey ? {
...eventForClients,
sessionKey
} : eventForClients;
const last = agentRunSeq.get(evt.runId) ?? 0;
const isToolEvent = evt.stream === "tool";
const toolVerbose = isToolEvent ? resolveToolVerboseLevel(evt.runId, sessionKey) : "off";
const toolPayload = isToolEvent && toolVerbose !== "full" ? (() => {
const data = evt.data ? { ...evt.data } : {};
delete data.result;
delete data.partialResult;
return sessionKey ? {
...eventForClients,
sessionKey,
data
} : {
...eventForClients,
data
};
})() : agentPayload;
if (last > 0 && evt.seq !== last + 1) broadcast("agent", {
runId: eventRunId,
stream: "error",
ts: Date.now(),
sessionKey,
data: {
reason: "seq gap",
expected: last + 1,
received: evt.seq
}
});
agentRunSeq.set(evt.runId, evt.seq);
if (isToolEvent) {
if ((typeof evt.data?.phase === "string" ? evt.data.phase : "") === "start" && isControlUiVisible && sessionKey && !isAborted) flushBufferedChatDeltaIfNeeded(sessionKey, clientRunId, evt.runId, evt.seq);
const recipients = toolEventRecipients.get(evt.runId);
if (recipients && recipients.size > 0) broadcastToConnIds("agent", toolPayload, recipients);
if (sessionKey) {
const sessionSubscribers = sessionEventSubscribers.getAll();
if (sessionSubscribers.size > 0) broadcastToConnIds("session.tool", toolPayload, sessionSubscribers, { dropIfSlow: true });
}
} else broadcast("agent", agentPayload);
const lifecyclePhase = evt.stream === "lifecycle" && typeof evt.data?.phase === "string" ? evt.data.phase : null;
if (isControlUiVisible && sessionKey) {
if (!isToolEvent || toolVerbose !== "off") nodeSendToSession(sessionKey, "agent", isToolEvent ? toolPayload : agentPayload);
if (!isAborted && evt.stream === "assistant" && typeof evt.data?.text === "string") emitChatDelta(sessionKey, clientRunId, evt.runId, evt.seq, evt.data.text, evt.data.delta);
else if (!isAborted && (lifecyclePhase === "end" || lifecyclePhase === "error")) {
const evtStopReason = typeof evt.data?.stopReason === "string" ? evt.data.stopReason : void 0;
if (chatLink) {
const finished = chatRunState.registry.shift(evt.runId);
if (!finished) {
clearAgentRunContext(evt.runId);
return;
}
emitChatFinal(finished.sessionKey, finished.clientRunId, evt.runId, evt.seq, lifecyclePhase === "error" ? "error" : "done", evt.data?.error, evtStopReason);
} else emitChatFinal(sessionKey, eventRunId, evt.runId, evt.seq, lifecyclePhase === "error" ? "error" : "done", evt.data?.error, evtStopReason);
} else if (isAborted && (lifecyclePhase === "end" || lifecyclePhase === "error")) {
chatRunState.abortedRuns.delete(clientRunId);
chatRunState.abortedRuns.delete(evt.runId);
chatRunState.buffers.delete(clientRunId);
chatRunState.deltaSentAt.delete(clientRunId);
if (chatLink) chatRunState.registry.remove(evt.runId, clientRunId, sessionKey);
}
}
if (lifecyclePhase === "end" || lifecyclePhase === "error") {
toolEventRecipients.markFinal(evt.runId);
clearAgentRunContext(evt.runId);
agentRunSeq.delete(evt.runId);
agentRunSeq.delete(clientRunId);
}
if (sessionKey && (lifecyclePhase === "start" || lifecyclePhase === "end" || lifecyclePhase === "error")) {
persistGatewaySessionLifecycleEvent({
sessionKey,
event: evt
}).catch(() => void 0);
const sessionEventConnIds = sessionEventSubscribers.getAll();
if (sessionEventConnIds.size > 0) broadcastToConnIds("sessions.changed", {
sessionKey,
phase: lifecyclePhase,
runId: evt.runId,
ts: evt.ts,
...buildSessionEventSnapshot(sessionKey, evt)
}, sessionEventConnIds, { dropIfSlow: true });
}
};
}
//#endregion
//#region src/hooks/gmail-watcher-errors.ts
const ADDRESS_IN_USE_RE = /address already in use|EADDRINUSE/i;
function isAddressInUseError(line) {
return ADDRESS_IN_USE_RE.test(line);
}
//#endregion
//#region src/hooks/gmail-watcher.ts
/**
* Gmail Watcher Service
*
* Automatically starts `gog gmail watch serve` when the gateway starts,
* if hooks.gmail is configured with an account.
*/
const log$3 = createSubsystemLogger("gmail-watcher");
let watcherProcess = null;
let renewInterval = null;
let shuttingDown = false;
let currentConfig = null;
/**
* Check if gog binary is available
*/
function isGogAvailable() {
return hasBinary("gog");
}
/**
* Start the Gmail watch (registers with Gmail API)
*/
async function startGmailWatch(cfg) {
const args = ["gog", ...buildGogWatchStartArgs(cfg)];
try {
const result = await runCommandWithTimeout(args, { timeoutMs: 12e4 });
if (result.code !== 0) {
const message = result.stderr || result.stdout || "gog watch start failed";
log$3.error(`watch start failed: ${message}`);
return false;
}
log$3.info(`watch started for ${cfg.account}`);
return true;
} catch (err) {
log$3.error(`watch start error: ${String(err)}`);
return false;
}
}
/**
* Spawn the gog gmail watch serve process
*/
function spawnGogServe(cfg) {
const args = buildGogWatchServeArgs(cfg);
log$3.info(`starting gog ${args.join(" ")}`);
let addressInUse = false;
const child = spawn("gog", args, {
stdio: [
"ignore",
"pipe",
"pipe"
],
detached: false
});
child.stdout?.on("data", (data) => {
const line = data.toString().trim();
if (line) log$3.info(`[gog] ${line}`);
});
child.stderr?.on("data", (data) => {
const line = data.toString().trim();
if (!line) return;
if (isAddressInUseError(line)) addressInUse = true;
log$3.warn(`[gog] ${line}`);
});
child.on("error", (err) => {
log$3.error(`gog process error: ${String(err)}`);
});
child.on("exit", (code, signal) => {
if (shuttingDown) return;
if (addressInUse) {
log$3.warn("gog serve failed to bind (address already in use); stopping restarts. Another watcher is likely running. Set OPENCLAW_SKIP_GMAIL_WATCHER=1 or stop the other process.");
watcherProcess = null;
return;
}
log$3.warn(`gog exited (code=${code}, signal=${signal}); restarting in 5s`);
watcherProcess = null;
setTimeout(() => {
if (shuttingDown || !currentConfig) return;
watcherProcess = spawnGogServe(currentConfig);
}, 5e3);
});
return child;
}
/**
* Start the Gmail watcher service.
* Called automatically by the gateway if hooks.gmail is configured.
*/
async function startGmailWatcher(cfg) {
if (!cfg.hooks?.enabled) return {
started: false,
reason: "hooks not enabled"
};
if (!cfg.hooks?.gmail?.account) return {
started: false,
reason: "no gmail account configured"
};
if (!isGogAvailable()) return {
started: false,
reason: "gog binary not found"
};
const resolved = resolveGmailHookRuntimeConfig(cfg, {});
if (!resolved.ok) return {
started: false,
reason: resolved.error
};
const runtimeConfig = resolved.value;
currentConfig = runtimeConfig;
if (runtimeConfig.tailscale.mode !== "off") try {
await ensureTailscaleEndpoint({
mode: runtimeConfig.tailscale.mode,
path: runtimeConfig.tailscale.path,
port: runtimeConfig.serve.port,
target: runtimeConfig.tailscale.target
});
log$3.info(`tailscale ${runtimeConfig.tailscale.mode} configured for port ${runtimeConfig.serve.port}`);
} catch (err) {
log$3.error(`tailscale setup failed: ${String(err)}`);
return {
started: false,
reason: `tailscale setup failed: ${String(err)}`
};
}
if (!await startGmailWatch(runtimeConfig)) log$3.warn("gmail watch start failed, but continuing with serve");
shuttingDown = false;
watcherProcess = spawnGogServe(runtimeConfig);
const renewMs = runtimeConfig.renewEveryMinutes * 6e4;
renewInterval = setInterval(() => {
if (shuttingDown) return;
startGmailWatch(runtimeConfig);
}, renewMs);
log$3.info(`gmail watcher started for ${runtimeConfig.account} (renew every ${runtimeConfig.renewEveryMinutes}m)`);
return { started: true };
}
/**
* Stop the Gmail watcher service.
*/
async function stopGmailWatcher() {
shuttingDown = true;
if (renewInterval) {
clearInterval(renewInterval);
renewInterval = null;
}
if (watcherProcess) {
log$3.info("stopping gmail watcher");
watcherProcess.kill("SIGTERM");
await new Promise((resolve) => {
const timeout = setTimeout(() => {
if (watcherProcess) watcherProcess.kill("SIGKILL");
resolve();
}, 3e3);
watcherProcess?.on("exit", () => {
clearTimeout(timeout);
resolve();
});
});
watcherProcess = null;
}
currentConfig = null;
log$3.info("gmail watcher stopped");
}
//#endregion
//#region src/gateway/server-close.ts
function createGatewayCloseHandler(params) {
return async (opts) => {
try {
const reason = (typeof opts?.reason === "string" ? opts.reason.trim() : "") || "gateway stopping";
const restartExpectedMs = typeof opts?.restartExpectedMs === "number" && Number.isFinite(opts.restartExpectedMs) ? Math.max(0, Math.floor(opts.restartExpectedMs)) : null;
if (params.bonjourStop) try {
await params.bonjourStop();
} catch {}
if (params.tailscaleCleanup) await params.tailscaleCleanup();
if (params.canvasHost) try {
await params.canvasHost.close();
} catch {}
if (params.canvasHostServer) try {
await params.canvasHostServer.close();
} catch {}
for (const plugin of listChannelPlugins()) await params.stopChannel(plugin.id);
if (params.pluginServices) await params.pluginServices.stop().catch(() => {});
await stopGmailWatcher();
params.cron.stop();
params.heartbeatRunner.stop();
try {
params.updateCheckStop?.();
} catch {}
for (const timer of params.nodePresenceTimers.values()) clearInterval(timer);
params.nodePresenceTimers.clear();
params.broadcast("shutdown", {
reason,
restartExpectedMs
});
clearInterval(params.tickInterval);
clearInterval(params.healthInterval);
clearInterval(params.dedupeCleanup);
if (params.mediaCleanup) clearInterval(params.mediaCleanup);
if (params.agentUnsub) try {
params.agentUnsub();
} catch {}
if (params.heartbeatUnsub) try {
params.heartbeatUnsub();
} catch {}
if (params.transcriptUnsub) try {
params.transcriptUnsub();
} catch {}
if (params.lifecycleUnsub) try {
params.lifecycleUnsub();
} catch {}
params.chatRunState.clear();
for (const c of params.clients) try {
c.socket.close(1012, "service restart");
} catch {}
params.clients.clear();
await params.configReloader.stop().catch(() => {});
if (params.browserControl) await params.browserControl.stop().catch(() => {});
await new Promise((resolve) => params.wss.close(() => resolve()));
const servers = params.httpServers && params.httpServers.length > 0 ? params.httpServers : [params.httpServer];
for (const server of servers) {
const httpServer = server;
if (typeof httpServer.closeIdleConnections === "function") httpServer.closeIdleConnections();
await new Promise((resolve, reject) => httpServer.close((err) => err ? reject(err) : resolve()));
}
} finally {
try {
params.releasePluginRouteRegistry?.();
} catch {}
}
};
}
//#endregion
//#region src/cron/isolated-agent/delivery-target.ts
async function resolveDeliveryTarget(cfg, agentId, jobPayload) {
const requestedChannel = typeof jobPayload.channel === "string" ? jobPayload.channel : "last";
const explicitTo = typeof jobPayload.to === "string" ? jobPayload.to : void 0;
const allowMismatchedLastTo = requestedChannel === "last";
const sessionCfg = cfg.session;
const mainSessionKey = resolveAgentMainSessionKey({
cfg,
agentId
});
const store = loadSessionStore(resolveStorePath(sessionCfg?.store, { agentId }));
const threadSessionKey = jobPayload.sessionKey?.trim();
const main = (threadSessionKey ? store[threadSessionKey] : void 0) ?? store[mainSessionKey];
const preliminary = resolveSessionDeliveryTarget({
entry: main,
requestedChannel,
explicitTo,
allowMismatchedLastTo
});
let fallbackChannel;
let channelResolutionError;
if (!preliminary.channel) if (preliminary.lastChannel) fallbackChannel = preliminary.lastChannel;
else try {
fallbackChannel = (await resolveMessageChannelSelection({ cfg })).channel;
} catch (err) {
const detail = err instanceof Error ? err.message : String(err);
channelResolutionError = /* @__PURE__ */ new Error(`${detail} Set delivery.channel explicitly or use a main session with a previous channel.`);
}
const resolved = fallbackChannel ? resolveSessionDeliveryTarget({
entry: main,
requestedChannel,
explicitTo,
fallbackChannel,
allowMismatchedLastTo,
mode: preliminary.mode
}) : preliminary;
const channel = resolved.channel ?? fallbackChannel;
const mode = resolved.mode;
let toCandidate = resolved.to;
let accountId = (typeof jobPayload.accountId === "string" && jobPayload.accountId.trim() ? jobPayload.accountId.trim() : void 0) ?? resolved.accountId;
if (!accountId && channel) {
const boundAccounts = buildChannelAccountBindings(cfg).get(channel)?.get(normalizeAgentId(agentId));
if (boundAccounts && boundAccounts.length > 0) accountId = boundAccounts[0];
}
if (jobPayload.accountId) accountId = jobPayload.accountId;
const threadId = resolved.threadId && (resolved.threadIdExplicit || resolved.to && resolved.to === resolved.lastTo) ? resolved.threadId : void 0;
if (!channel) return {
ok: false,
channel: void 0,
to: void 0,
accountId,
threadId,
mode,
error: channelResolutionError ?? /* @__PURE__ */ new Error("Channel is required when delivery.channel=last has no previous channel.")
};
let allowFromOverride;
if (channel === "whatsapp") {
const resolvedAccountId = normalizeAccountId$1(accountId);
const configuredAllowFrom = (resolveWhatsAppAccount({
cfg,
accountId: resolvedAccountId
}).allowFrom ?? []).map((entry) => String(entry).trim()).filter((entry) => entry && entry !== "*").map((entry) => normalizeWhatsAppTarget(entry)).filter((entry) => Boolean(entry));
const storeAllowFrom = readChannelAllowFromStoreSync("whatsapp", process.env, resolvedAccountId).map((entry) => normalizeWhatsAppTarget(entry)).filter((entry) => Boolean(entry));
allowFromOverride = [...new Set([...configuredAllowFrom, ...storeAllowFrom])];
if (toCandidate && mode === "implicit" && allowFromOverride.length > 0) {
const normalizedCurrentTarget = normalizeWhatsAppTarget(toCandidate);
if (!normalizedCurrentTarget || !allowFromOverride.includes(normalizedCurrentTarget)) toCandidate = allowFromOverride[0];
}
}
const docked = resolveOutboundTarget({
channel,
to: toCandidate,
cfg,
accountId,
mode,
allowFrom: allowFromOverride
});
if (!docked.ok) return {
ok: false,
channel,
to: void 0,
accountId,
threadId,
mode,
error: docked.error
};
return {
ok: true,
channel,
to: (await maybeResolveIdLikeTarget({
cfg,
channel,
input: docked.to,
accountId
}))?.to ?? docked.to,
accountId,
threadId,
mode
};
}
//#endregion
//#region src/cron/delivery.ts
function normalizeChannel(value) {
if (typeof value !== "string") return;
const trimmed = value.trim().toLowerCase();
if (!trimmed) return;
return trimmed;
}
function normalizeTo$1(value) {
if (typeof value !== "string") return;
const trimmed = value.trim();
return trimmed ? trimmed : void 0;
}
function normalizeAccountId(value) {
if (typeof value !== "string") return;
const trimmed = value.trim();
return trimmed ? trimmed : void 0;
}
function resolveCronDeliveryPlan(job) {
const payload = job.payload.kind === "agentTurn" ? job.payload : null;
const delivery = job.delivery;
const hasDelivery = delivery && typeof delivery === "object";
const rawMode = hasDelivery ? delivery.mode : void 0;
const normalizedMode = typeof rawMode === "string" ? rawMode.trim().toLowerCase() : rawMode;
const mode = normalizedMode === "announce" ? "announce" : normalizedMode === "webhook" ? "webhook" : normalizedMode === "none" ? "none" : normalizedMode === "deliver" ? "announce" : void 0;
const payloadChannel = normalizeChannel(payload?.channel);
const payloadTo = normalizeTo$1(payload?.to);
const deliveryChannel = normalizeChannel(delivery?.channel);
const deliveryTo = normalizeTo$1(delivery?.to);
const channel = deliveryChannel ?? payloadChannel ?? "last";
const to = deliveryTo ?? payloadTo;
const deliveryAccountId = normalizeAccountId(delivery?.accountId);
if (hasDelivery) {
const resolvedMode = mode ?? "announce";
return {
mode: resolvedMode,
channel: resolvedMode === "announce" ? channel : void 0,
to,
accountId: deliveryAccountId,
source: "delivery",
requested: resolvedMode === "announce"
};
}
const legacyMode = payload?.deliver === true ? "explicit" : payload?.deliver === false ? "off" : "auto";
const requested = legacyMode === "explicit" || legacyMode === "auto" && Boolean(to);
return {
mode: requested ? "announce" : "none",
channel,
to,
source: "payload",
requested
};
}
function normalizeFailureMode(value) {
if (typeof value !== "string") return;
const trimmed = value.trim().toLowerCase();
if (trimmed === "announce" || trimmed === "webhook") return trimmed;
}
function resolveFailureDestination(job, globalConfig) {
const delivery = job.delivery;
const jobFailureDest = delivery?.failureDestination;
const hasJobFailureDest = jobFailureDest && typeof jobFailureDest === "object";
let channel;
let to;
let accountId;
let mode;
if (globalConfig) {
channel = normalizeChannel(globalConfig.channel);
to = normalizeTo$1(globalConfig.to);
accountId = normalizeAccountId(globalConfig.accountId);
mode = normalizeFailureMode(globalConfig.mode);
}
if (hasJobFailureDest) {
const jobChannel = normalizeChannel(jobFailureDest.channel);
const jobTo = normalizeTo$1(jobFailureDest.to);
const jobAccountId = normalizeAccountId(jobFailureDest.accountId);
const jobMode = normalizeFailureMode(jobFailureDest.mode);
const hasJobChannelField = "channel" in jobFailureDest;
const hasJobToField = "to" in jobFailureDest;
const hasJobAccountIdField = "accountId" in jobFailureDest;
const jobToExplicitValue = hasJobToField && jobTo !== void 0;
if (hasJobChannelField) channel = jobChannel;
if (hasJobToField) to = jobTo;
if (hasJobAccountIdField) accountId = jobAccountId;
if (jobMode !== void 0) {
const globalMode = globalConfig?.mode ?? "announce";
if (!jobToExplicitValue && globalMode !== jobMode) to = void 0;
mode = jobMode;
}
}
if (!channel && !to && !accountId && !mode) return null;
const resolvedMode = mode ?? "announce";
if (resolvedMode === "webhook" && !to) return null;
const result = {
mode: resolvedMode,
channel: resolvedMode === "announce" ? channel ?? "last" : void 0,
to,
accountId
};
if (delivery && isSameDeliveryTarget(delivery, result)) return null;
return result;
}
function isSameDeliveryTarget(delivery, failurePlan) {
const primaryMode = delivery.mode ?? "announce";
if (primaryMode === "none") return false;
const primaryChannel = delivery.channel;
const primaryTo = delivery.to;
const primaryAccountId = delivery.accountId;
if (failurePlan.mode === "webhook") return primaryMode === "webhook" && primaryTo === failurePlan.to;
const primaryChannelNormalized = primaryChannel ?? "last";
return (failurePlan.channel ?? "last") === primaryChannelNormalized && failurePlan.to === primaryTo && failurePlan.accountId === primaryAccountId;
}
const FAILURE_NOTIFICATION_TIMEOUT_MS = 3e4;
const cronDeliveryLogger = getChildLogger({ subsystem: "cron-delivery" });
async function sendFailureNotificationAnnounce(deps, cfg, agentId, jobId, target, message) {
const resolvedTarget = await resolveDeliveryTarget(cfg, agentId, {
channel: target.channel,
to: target.to,
accountId: target.accountId
});
if (!resolvedTarget.ok) {
cronDeliveryLogger.warn({ error: resolvedTarget.error.message }, "cron: failed to resolve failure destination target");
return;
}
const identity = resolveAgentOutboundIdentity(cfg, agentId);
const session = buildOutboundSessionContext({
cfg,
agentId,
sessionKey: `cron:${jobId}:failure`
});
const abortController = new AbortController();
const timeout = setTimeout(() => {
abortController.abort();
}, FAILURE_NOTIFICATION_TIMEOUT_MS);
try {
await deliverOutboundPayloads({
cfg,
channel: resolvedTarget.channel,
to: resolvedTarget.to,
accountId: resolvedTarget.accountId,
threadId: resolvedTarget.threadId,
payloads: [{ text: message }],
session,
identity,
bestEffort: false,
deps: createOutboundSendDeps(deps),
abortSignal: abortController.signal
});
} catch (err) {
cronDeliveryLogger.warn({
err: formatErrorMessage(err),
channel: resolvedTarget.channel,
to: resolvedTarget.to
}, "cron: failure destination announce failed");
} finally {
clearTimeout(timeout);
}
}
//#endregion
//#region src/cron/heartbeat-policy.ts
function shouldSkipHeartbeatOnlyDelivery(payloads, ackMaxChars) {
if (payloads.length === 0) return true;
if (payloads.some((payload) => resolveSendableOutboundReplyParts(payload).hasMedia)) return false;
return payloads.some((payload) => {
return stripHeartbeatToken(payload.text, {
mode: "heartbeat",
maxAckChars: ackMaxChars
}).shouldSkip;
});
}
//#endregion
//#region src/cron/isolated-agent/helpers.ts
function pickSummaryFromOutput(text) {
const clean = (text ?? "").trim();
if (!clean) return;
const limit = 2e3;
return clean.length > limit ? `${truncateUtf16Safe(clean, limit)}…` : clean;
}
function pickSummaryFromPayloads(payloads) {
for (let i = payloads.length - 1; i >= 0; i--) {
if (payloads[i]?.isError) continue;
const summary = pickSummaryFromOutput(payloads[i]?.text);
if (summary) return summary;
}
for (let i = payloads.length - 1; i >= 0; i--) {
const summary = pickSummaryFromOutput(payloads[i]?.text);
if (summary) return summary;
}
}
function pickLastNonEmptyTextFromPayloads(payloads) {
for (let i = payloads.length - 1; i >= 0; i--) {
if (payloads[i]?.isError) continue;
const clean = (payloads[i]?.text ?? "").trim();
if (clean) return clean;
}
for (let i = payloads.length - 1; i >= 0; i--) {
const clean = (payloads[i]?.text ?? "").trim();
if (clean) return clean;
}
}
function pickLastDeliverablePayload(payloads) {
const isDeliverable = (p) => {
const hasInteractive = (p?.interactive?.blocks?.length ?? 0) > 0;
const hasChannelData = Object.keys(p?.channelData ?? {}).length > 0;
return hasOutboundReplyContent(p, { trimText: true }) || hasInteractive || hasChannelData;
};
for (let i = payloads.length - 1; i >= 0; i--) {
if (payloads[i]?.isError) continue;
if (isDeliverable(payloads[i])) return payloads[i];
}
for (let i = payloads.length - 1; i >= 0; i--) if (isDeliverable(payloads[i])) return payloads[i];
}
/**
* Check if delivery should be skipped because the agent signaled no user-visible update.
* Returns true when any payload is a heartbeat ack token and no payload contains media.
*/
function isHeartbeatOnlyResponse(payloads, ackMaxChars) {
return shouldSkipHeartbeatOnlyDelivery(payloads, ackMaxChars);
}
function resolveHeartbeatAckMaxChars(agentCfg) {
const raw = agentCfg?.heartbeat?.ackMaxChars ?? 300;
return Math.max(0, raw);
}
function resolveCronPayloadOutcome(params) {
const firstText = params.payloads[0]?.text ?? "";
const summary = pickSummaryFromPayloads(params.payloads) ?? pickSummaryFromOutput(firstText);
const outputText = pickLastNonEmptyTextFromPayloads(params.payloads);
const synthesizedText = outputText?.trim() || summary?.trim() || void 0;
const deliveryPayload = pickLastDeliverablePayload(params.payloads);
const deliveryPayloads = deliveryPayload !== void 0 ? [deliveryPayload] : synthesizedText ? [{ text: synthesizedText }] : [];
const deliveryPayloadHasStructuredContent = deliveryPayload?.mediaUrl !== void 0 || (deliveryPayload?.mediaUrls?.length ?? 0) > 0 || (deliveryPayload?.interactive?.blocks?.length ?? 0) > 0 || Object.keys(deliveryPayload?.channelData ?? {}).length > 0;
const hasErrorPayload = params.payloads.some((payload) => payload?.isError === true);
const lastErrorPayloadIndex = params.payloads.findLastIndex((payload) => payload?.isError === true);
const hasSuccessfulPayloadAfterLastError = !params.runLevelError && lastErrorPayloadIndex >= 0 && params.payloads.slice(lastErrorPayloadIndex + 1).some((payload) => payload?.isError !== true && Boolean(payload?.text?.trim()));
const hasFatalErrorPayload = hasErrorPayload && !hasSuccessfulPayloadAfterLastError;
const lastErrorPayloadText = [...params.payloads].toReversed().find((payload) => payload?.isError === true && Boolean(payload?.text?.trim()))?.text?.trim();
return {
summary,
outputText,
synthesizedText,
deliveryPayload,
deliveryPayloads,
deliveryPayloadHasStructuredContent,
hasFatalErrorPayload,
embeddedRunError: hasFatalErrorPayload ? lastErrorPayloadText ?? "cron isolated run returned an error payload" : void 0
};
}
//#endregion
//#region src/cron/isolated-agent/subagent-followup.ts
function resolveCronSubagentTimings() {
const fastTestMode = process.env.OPENCLAW_TEST_FAST === "1";
return {
waitMinMs: fastTestMode ? 10 : 3e4,
finalReplyGraceMs: fastTestMode ? 50 : 5e3,
gracePollMs: fastTestMode ? 8 : 200
};
}
const SUBAGENT_FOLLOWUP_HINTS = [
"subagent spawned",
"spawned a subagent",
"auto-announce when done",
"both subagents are running",
"wait for them to report back"
];
const INTERIM_CRON_HINTS = [
"on it",
"pulling everything together",
"give me a few",
"give me a few min",
"few minutes",
"let me compile",
"i'll gather",
"i will gather",
"working on it",
"retrying now",
"should be about",
"should have your summary",
"it'll auto-announce when done",
"it will auto-announce when done",
...SUBAGENT_FOLLOWUP_HINTS
];
function normalizeHintText(value) {
return value.trim().toLowerCase().replace(/\s+/g, " ");
}
function isLikelyInterimCronMessage(value) {
const normalized = normalizeHintText(value);
if (!normalized) return false;
return normalized.split(" ").filter(Boolean).length <= 45 && INTERIM_CRON_HINTS.some((hint) => normalized.includes(hint));
}
function expectsSubagentFollowup(value) {
const normalized = normalizeHintText(value);
return Boolean(normalized && SUBAGENT_FOLLOWUP_HINTS.some((hint) => normalized.includes(hint)));
}
async function readDescendantSubagentFallbackReply(params) {
const descendants = listDescendantRunsForRequester(params.sessionKey).filter((entry) => typeof entry.endedAt === "number" && entry.endedAt >= params.runStartedAt && entry.childSessionKey.trim().length > 0).toSorted((a, b) => (a.endedAt ?? 0) - (b.endedAt ?? 0));
if (descendants.length === 0) return;
const latestByChild = /* @__PURE__ */ new Map();
for (const entry of descendants) {
const childKey = entry.childSessionKey.trim();
if (!childKey) continue;
const current = latestByChild.get(childKey);
if (!current || (entry.endedAt ?? 0) >= (current.endedAt ?? 0)) latestByChild.set(childKey, entry);
}
const replies = [];
const latestRuns = [...latestByChild.values()].toSorted((a, b) => (a.endedAt ?? 0) - (b.endedAt ?? 0)).slice(-4);
for (const entry of latestRuns) {
let reply = (await readLatestAssistantReply({ sessionKey: entry.childSessionKey }))?.trim();
if (!reply && typeof entry.frozenResultText === "string" && entry.frozenResultText.trim()) reply = entry.frozenResultText.trim();
if (!reply || reply.toUpperCase() === "NO_REPLY".toUpperCase()) continue;
replies.push(reply);
}
if (replies.length === 0) return;
if (replies.length === 1) return replies[0];
return replies.join("\n\n");
}
/**
* Waits for descendant subagents to complete using a push-based approach:
* each active descendant run is awaited via `agent.wait` (gateway RPC) instead
* of a busy-poll loop. After all active runs settle, a short grace period
* polls the cron agent's session for a post-orchestration synthesis message.
*/
async function waitForDescendantSubagentSummary(params) {
const timings = resolveCronSubagentTimings();
const initialReply = params.initialReply?.trim();
const deadline = Date.now() + Math.max(timings.waitMinMs, Math.floor(params.timeoutMs));
const getActiveRuns = () => listDescendantRunsForRequester(params.sessionKey).filter((entry) => typeof entry.endedAt !== "number");
const initialActiveRuns = getActiveRuns();
if (!(params.observedActiveDescendants === true || initialActiveRuns.length > 0)) return initialReply;
let pendingRunIds = new Set(initialActiveRuns.map((e) => e.runId));
while (pendingRunIds.size > 0 && Date.now() < deadline) {
const remainingMs = Math.max(1, deadline - Date.now());
await Promise.allSettled([...pendingRunIds].map((runId) => callGateway({
method: "agent.wait",
params: {
runId,
timeoutMs: remainingMs
},
timeoutMs: remainingMs + 2e3
}).catch(() => void 0)));
pendingRunIds = new Set(getActiveRuns().map((e) => e.runId));
}
const gracePeriodDeadline = Math.min(Date.now() + timings.finalReplyGraceMs, deadline);
const resolveUsableLatestReply = async () => {
const latest = (await readLatestAssistantReply({ sessionKey: params.sessionKey }))?.trim();
if (latest && latest.toUpperCase() !== "NO_REPLY".toUpperCase() && (latest !== initialReply || !isLikelyInterimCronMessage(latest))) return latest;
};
while (Date.now() < gracePeriodDeadline) {
const latest = await resolveUsableLatestReply();
if (latest) return latest;
await new Promise((resolve) => setTimeout(resolve, timings.gracePollMs));
}
const latest = await resolveUsableLatestReply();
if (latest) return latest;
}
//#endregion
//#region src/cron/isolated-agent/delivery-dispatch.ts
function normalizeDeliveryTarget(channel, to) {
const channelLower = channel.trim().toLowerCase();
const toTrimmed = to.trim();
if (channelLower === "feishu" || channelLower === "lark") {
const lowered = toTrimmed.toLowerCase();
if (lowered.startsWith("user:")) return toTrimmed.slice(5).trim();
if (lowered.startsWith("chat:")) return toTrimmed.slice(5).trim();
}
return toTrimmed;
}
function matchesMessagingToolDeliveryTarget(target, delivery) {
if (!delivery.channel || !delivery.to || !target.to) return false;
const channel = delivery.channel.trim().toLowerCase();
const provider = target.provider?.trim().toLowerCase();
if (provider && provider !== "message" && provider !== channel) return false;
if (target.accountId && delivery.accountId && target.accountId !== delivery.accountId) return false;
return normalizeDeliveryTarget(channel, target.to.replace(/:topic:\d+$/, "")) === normalizeDeliveryTarget(channel, delivery.to);
}
function resolveCronDeliveryBestEffort(job) {
if (typeof job.delivery?.bestEffort === "boolean") return job.delivery.bestEffort;
if (job.payload.kind === "agentTurn" && typeof job.payload.bestEffortDeliver === "boolean") return job.payload.bestEffortDeliver;
return false;
}
const TRANSIENT_DIRECT_CRON_DELIVERY_ERROR_PATTERNS = [
/\berrorcode=unavailable\b/i,
/\bstatus\s*[:=]\s*"?unavailable\b/i,
/\bUNAVAILABLE\b/,
/no active .* listener/i,
/gateway not connected/i,
/gateway closed \(1006/i,
/gateway timeout/i,
/\b(econnreset|econnrefused|etimedout|enotfound|ehostunreach|network error)\b/i
];
const PERMANENT_DIRECT_CRON_DELIVERY_ERROR_PATTERNS = [
/unsupported channel/i,
/unknown channel/i,
/chat not found/i,
/user not found/i,
/bot was blocked by the user/i,
/forbidden: bot was kicked/i,
/recipient is not a valid/i,
/outbound not configured for channel/i
];
const STALE_CRON_DELIVERY_MAX_START_DELAY_MS = 180 * 6e4;
const COMPLETED_DIRECT_CRON_DELIVERIES = /* @__PURE__ */ new Map();
function cloneDeliveryResults(results) {
return results.map((result) => ({
...result,
...result.meta ? { meta: { ...result.meta } } : {}
}));
}
function pruneCompletedDirectCronDeliveries(now) {
const ttlMs = process.env.OPENCLAW_TEST_FAST === "1" ? 6e4 : 1440 * 60 * 1e3;
for (const [key, entry] of COMPLETED_DIRECT_CRON_DELIVERIES) if (now - entry.ts >= ttlMs) COMPLETED_DIRECT_CRON_DELIVERIES.delete(key);
const maxEntries = 2e3;
if (COMPLETED_DIRECT_CRON_DELIVERIES.size <= maxEntries) return;
const entries = [...COMPLETED_DIRECT_CRON_DELIVERIES.entries()].toSorted((a, b) => a[1].ts - b[1].ts);
const toDelete = COMPLETED_DIRECT_CRON_DELIVERIES.size - maxEntries;
for (let i = 0; i < toDelete; i += 1) {
const oldest = entries[i];
if (!oldest) break;
COMPLETED_DIRECT_CRON_DELIVERIES.delete(oldest[0]);
}
}
function resolveCronDeliveryScheduledAtMs(params) {
const scheduledAt = params.job.state?.nextRunAtMs;
return typeof scheduledAt === "number" && Number.isFinite(scheduledAt) ? scheduledAt : params.runStartedAt;
}
function resolveCronDeliveryStartDelayMs(params) {
return params.runStartedAt - resolveCronDeliveryScheduledAtMs(params);
}
function isStaleCronDelivery(params) {
return resolveCronDeliveryStartDelayMs(params) > STALE_CRON_DELIVERY_MAX_START_DELAY_MS;
}
function rememberCompletedDirectCronDelivery(idempotencyKey, results) {
const now = Date.now();
COMPLETED_DIRECT_CRON_DELIVERIES.set(idempotencyKey, {
ts: now,
results: cloneDeliveryResults(results)
});
pruneCompletedDirectCronDeliveries(now);
}
function getCompletedDirectCronDelivery(idempotencyKey) {
pruneCompletedDirectCronDeliveries(Date.now());
const cached = COMPLETED_DIRECT_CRON_DELIVERIES.get(idempotencyKey);
if (!cached) return;
return cloneDeliveryResults(cached.results);
}
function buildDirectCronDeliveryIdempotencyKey(params) {
const threadId = params.delivery.threadId == null || params.delivery.threadId === "" ? "" : String(params.delivery.threadId);
const accountId = params.delivery.accountId?.trim() ?? "";
const normalizedTo = normalizeDeliveryTarget(params.delivery.channel, params.delivery.to);
return `cron-direct-delivery:v1:${params.runSessionId}:${params.delivery.channel}:${accountId}:${normalizedTo}:${threadId}`;
}
function summarizeDirectCronDeliveryError(error) {
if (error instanceof Error) return error.message || "error";
if (typeof error === "string") return error;
try {
return JSON.stringify(error) || String(error);
} catch {
return String(error);
}
}
function isTransientDirectCronDeliveryError(error) {
const message = summarizeDirectCronDeliveryError(error);
if (!message) return false;
if (PERMANENT_DIRECT_CRON_DELIVERY_ERROR_PATTERNS.some((re) => re.test(message))) return false;
return TRANSIENT_DIRECT_CRON_DELIVERY_ERROR_PATTERNS.some((re) => re.test(message));
}
function resolveDirectCronRetryDelaysMs() {
return [
5e3,
1e4,
2e4
];
}
async function retryTransientDirectCronDelivery(params) {
const retryDelaysMs = resolveDirectCronRetryDelaysMs();
let retryIndex = 0;
for (;;) {
if (params.signal?.aborted) throw new Error("cron delivery aborted");
try {
return await params.run();
} catch (err) {
const delayMs = retryDelaysMs[retryIndex];
if (delayMs == null || !isTransientDirectCronDeliveryError(err) || params.signal?.aborted) throw err;
const nextAttempt = retryIndex + 2;
const maxAttempts = retryDelaysMs.length + 1;
logWarn(`[cron:${params.jobId}] transient direct announce delivery failure, retrying ${nextAttempt}/${maxAttempts} in ${Math.round(delayMs / 1e3)}s: ${summarizeDirectCronDeliveryError(err)}`);
retryIndex += 1;
await sleepWithAbort(delayMs, params.signal);
}
}
}
async function dispatchCronDelivery(params) {
const skipMessagingToolDelivery = params.skipMessagingToolDelivery === true;
let summary = params.summary;
let outputText = params.outputText;
let synthesizedText = params.synthesizedText;
let deliveryPayloads = params.deliveryPayloads;
let delivered = skipMessagingToolDelivery;
let deliveryAttempted = skipMessagingToolDelivery;
const failDeliveryTarget = (error) => params.withRunSession({
status: "error",
error,
errorKind: "delivery-target",
summary,
outputText,
deliveryAttempted,
...params.telemetry
});
const deliverViaDirect = async (delivery, options) => {
const identity = resolveAgentOutboundIdentity(params.cfgWithAgentDefaults, params.agentId);
const deliveryIdempotencyKey = buildDirectCronDeliveryIdempotencyKey({
runSessionId: params.runSessionId,
delivery
});
try {
const payloadsForDelivery = deliveryPayloads.length > 0 ? deliveryPayloads : synthesizedText ? [{ text: synthesizedText }] : [];
if (payloadsForDelivery.length === 0) return null;
if (params.isAborted()) return params.withRunSession({
status: "error",
error: params.abortReason(),
deliveryAttempted,
...params.telemetry
});
if (params.deliveryRequested && isStaleCronDelivery({
job: params.job,
runStartedAt: params.runStartedAt
})) {
deliveryAttempted = true;
const nowMs = Date.now();
const scheduledAtMs = resolveCronDeliveryScheduledAtMs({
job: params.job,
runStartedAt: params.runStartedAt
});
const startDelayMs = resolveCronDeliveryStartDelayMs({
job: params.job,
runStartedAt: params.runStartedAt
});
logWarn(`[cron:${params.job.id}] skipping stale delivery scheduled at ${new Date(scheduledAtMs).toISOString()}, started ${Math.round(startDelayMs / 6e4)}m late, current age ${Math.round((nowMs - scheduledAtMs) / 6e4)}m`);
return params.withRunSession({
status: "ok",
summary,
outputText,
deliveryAttempted,
delivered: false,
...params.telemetry
});
}
deliveryAttempted = true;
if (getCompletedDirectCronDelivery(deliveryIdempotencyKey)) {
delivered = true;
return null;
}
const deliverySession = buildOutboundSessionContext({
cfg: params.cfgWithAgentDefaults,
agentId: params.agentId,
sessionKey: params.agentSessionKey
});
const runDelivery = async () => await deliverOutboundPayloads({
cfg: params.cfgWithAgentDefaults,
channel: delivery.channel,
to: delivery.to,
accountId: delivery.accountId,
threadId: delivery.threadId,
payloads: payloadsForDelivery,
session: deliverySession,
identity,
bestEffort: params.deliveryBestEffort,
deps: createOutboundSendDeps(params.deps),
abortSignal: params.abortSignal,
skipQueue: true
});
const deliveryResults = options?.retryTransient ? await retryTransientDirectCronDelivery({
jobId: params.job.id,
signal: params.abortSignal,
run: runDelivery
}) : await runDelivery();
delivered = deliveryResults.length > 0;
if (delivered) rememberCompletedDirectCronDelivery(deliveryIdempotencyKey, deliveryResults);
return null;
} catch (err) {
if (!params.deliveryBestEffort) return params.withRunSession({
status: "error",
summary,
outputText,
error: String(err),
deliveryAttempted,
...params.telemetry
});
return null;
}
};
const finalizeTextDelivery = async (delivery) => {
const cleanupDirectCronSessionIfNeeded = async () => {
if (!params.job.deleteAfterRun) return;
try {
await callGateway({
method: "sessions.delete",
params: {
key: params.agentSessionKey,
deleteTranscript: true,
emitLifecycleHooks: false
},
timeoutMs: 1e4
});
} catch {}
};
if (!synthesizedText) return null;
const initialSynthesizedText = synthesizedText.trim();
let activeSubagentRuns = countActiveDescendantRuns(params.agentSessionKey);
const expectedSubagentFollowup = expectsSubagentFollowup(initialSynthesizedText);
const completedDescendantReply = activeSubagentRuns === 0 && isLikelyInterimCronMessage(initialSynthesizedText) ? await readDescendantSubagentFallbackReply({
sessionKey: params.agentSessionKey,
runStartedAt: params.runStartedAt
}) : void 0;
const hadDescendants = activeSubagentRuns > 0 || Boolean(completedDescendantReply);
if (activeSubagentRuns > 0 || expectedSubagentFollowup) {
let finalReply = await waitForDescendantSubagentSummary({
sessionKey: params.agentSessionKey,
initialReply: initialSynthesizedText,
timeoutMs: params.timeoutMs,
observedActiveDescendants: activeSubagentRuns > 0 || expectedSubagentFollowup
});
activeSubagentRuns = countActiveDescendantRuns(params.agentSessionKey);
if (!finalReply && activeSubagentRuns === 0) finalReply = await readDescendantSubagentFallbackReply({
sessionKey: params.agentSessionKey,
runStartedAt: params.runStartedAt
});
if (finalReply && activeSubagentRuns === 0) {
outputText = finalReply;
summary = pickSummaryFromOutput(finalReply) ?? summary;
synthesizedText = finalReply;
deliveryPayloads = [{ text: finalReply }];
}
} else if (completedDescendantReply) {
outputText = completedDescendantReply;
summary = pickSummaryFromOutput(completedDescendantReply) ?? summary;
synthesizedText = completedDescendantReply;
deliveryPayloads = [{ text: completedDescendantReply }];
}
if (activeSubagentRuns > 0) {
deliveryAttempted = true;
return params.withRunSession({
status: "ok",
summary,
outputText,
deliveryAttempted,
...params.telemetry
});
}
if (hadDescendants && synthesizedText.trim() === initialSynthesizedText && isLikelyInterimCronMessage(initialSynthesizedText) && initialSynthesizedText.toUpperCase() !== "NO_REPLY".toUpperCase()) {
deliveryAttempted = true;
return params.withRunSession({
status: "ok",
summary,
outputText,
deliveryAttempted,
...params.telemetry
});
}
if (synthesizedText.toUpperCase() === "NO_REPLY".toUpperCase()) return params.withRunSession({
status: "ok",
summary,
outputText,
delivered: true,
...params.telemetry
});
if (params.isAborted()) return params.withRunSession({
status: "error",
error: params.abortReason(),
deliveryAttempted,
...params.telemetry
});
try {
return await deliverViaDirect(delivery, { retryTransient: true });
} finally {
await cleanupDirectCronSessionIfNeeded();
}
};
if (params.deliveryRequested && !params.skipHeartbeatDelivery && !skipMessagingToolDelivery) {
if (!params.resolvedDelivery.ok) {
if (!params.deliveryBestEffort) return {
result: failDeliveryTarget(params.resolvedDelivery.error.message),
delivered,
deliveryAttempted,
summary,
outputText,
synthesizedText,
deliveryPayloads
};
logWarn(`[cron:${params.job.id}] ${params.resolvedDelivery.error.message}`);
return {
result: params.withRunSession({
status: "ok",
summary,
outputText,
deliveryAttempted,
...params.telemetry
}),
delivered,
deliveryAttempted,
summary,
outputText,
synthesizedText,
deliveryPayloads
};
}
if (params.deliveryPayloadHasStructuredContent || params.resolvedDelivery.threadId != null) {
const directResult = await deliverViaDirect(params.resolvedDelivery);
if (directResult) return {
result: directResult,
delivered,
deliveryAttempted,
summary,
outputText,
synthesizedText,
deliveryPayloads
};
} else {
const finalizedTextResult = await finalizeTextDelivery(params.resolvedDelivery);
if (finalizedTextResult) return {
result: finalizedTextResult,
delivered,
deliveryAttempted,
summary,
outputText,
synthesizedText,
deliveryPayloads
};
}
}
return {
delivered,
deliveryAttempted,
summary,
outputText,
synthesizedText,
deliveryPayloads
};
}
//#endregion
//#region src/cron/isolated-agent/model-selection.ts
async function resolveCronModelSelection(params) {
const resolvedDefault = resolveConfiguredModelRef({
cfg: params.cfgWithAgentDefaults,
defaultProvider: DEFAULT_PROVIDER,
defaultModel: DEFAULT_MODEL
});
let provider = resolvedDefault.provider;
let model = resolvedDefault.model;
let catalog;
const loadCatalogOnce = async () => {
if (!catalog) catalog = await loadModelCatalog({ config: params.cfgWithAgentDefaults });
return catalog;
};
const subagentModelRaw = normalizeModelSelection(params.agentConfigOverride?.subagents?.model) ?? normalizeModelSelection(params.cfg.agents?.defaults?.subagents?.model);
if (subagentModelRaw) {
const resolvedSubagent = resolveAllowedModelRef({
cfg: params.cfgWithAgentDefaults,
catalog: await loadCatalogOnce(),
raw: subagentModelRaw,
defaultProvider: resolvedDefault.provider,
defaultModel: resolvedDefault.model
});
if (!("error" in resolvedSubagent)) {
provider = resolvedSubagent.ref.provider;
model = resolvedSubagent.ref.model;
}
}
let hooksGmailModelApplied = false;
const hooksGmailModelRef = params.isGmailHook ? resolveHooksGmailModel({
cfg: params.cfg,
defaultProvider: DEFAULT_PROVIDER
}) : null;
if (hooksGmailModelRef) {
if (getModelRefStatus({
cfg: params.cfg,
catalog: await loadCatalogOnce(),
ref: hooksGmailModelRef,
defaultProvider: resolvedDefault.provider,
defaultModel: resolvedDefault.model
}).allowed) {
provider = hooksGmailModelRef.provider;
model = hooksGmailModelRef.model;
hooksGmailModelApplied = true;
}
}
const modelOverrideRaw = params.payload.kind === "agentTurn" ? params.payload.model : void 0;
const modelOverride = typeof modelOverrideRaw === "string" ? modelOverrideRaw.trim() : void 0;
if (modelOverride !== void 0 && modelOverride.length > 0) {
const resolvedOverride = resolveAllowedModelRef({
cfg: params.cfgWithAgentDefaults,
catalog: await loadCatalogOnce(),
raw: modelOverride,
defaultProvider: resolvedDefault.provider,
defaultModel: resolvedDefault.model
});
if ("error" in resolvedOverride) {
if (resolvedOverride.error.startsWith("model not allowed:")) return {
ok: true,
provider,
model,
warning: `cron: payload.model '${modelOverride}' not allowed, falling back to agent defaults`
};
return {
ok: false,
error: resolvedOverride.error
};
}
provider = resolvedOverride.ref.provider;
model = resolvedOverride.ref.model;
}
if (!modelOverride && !hooksGmailModelApplied) {
const sessionModelOverride = params.sessionEntry.modelOverride?.trim();
if (sessionModelOverride) {
const sessionProviderOverride = params.sessionEntry.providerOverride?.trim() || resolvedDefault.provider;
const resolvedSessionOverride = resolveAllowedModelRef({
cfg: params.cfgWithAgentDefaults,
catalog: await loadCatalogOnce(),
raw: `${sessionProviderOverride}/${sessionModelOverride}`,
defaultProvider: resolvedDefault.provider,
defaultModel: resolvedDefault.model
});
if (!("error" in resolvedSessionOverride)) {
provider = resolvedSessionOverride.ref.provider;
model = resolvedSessionOverride.ref.model;
}
}
}
return {
ok: true,
provider,
model
};
}
//#endregion
//#region src/cron/isolated-agent/run-config.ts
function extractCronAgentDefaultsOverride(agentConfigOverride) {
const { model: overrideModel, sandbox: _agentSandboxOverride, ...agentOverrideRest } = agentConfigOverride ?? {};
return {
overrideModel,
definedOverrides: Object.fromEntries(Object.entries(agentOverrideRest).filter(([, value]) => value !== void 0))
};
}
function mergeCronAgentModelOverride(params) {
const nextDefaults = { ...params.defaults };
const existingModel = nextDefaults.model && typeof nextDefaults.model === "object" ? nextDefaults.model : {};
if (typeof params.overrideModel === "string") nextDefaults.model = {
...existingModel,
primary: params.overrideModel
};
else if (params.overrideModel) nextDefaults.model = {
...existingModel,
...params.overrideModel
};
return nextDefaults;
}
function buildCronAgentDefaultsConfig(params) {
const { overrideModel, definedOverrides } = extractCronAgentDefaultsOverride(params.agentConfigOverride);
return mergeCronAgentModelOverride({
defaults: Object.assign({}, params.defaults, definedOverrides),
overrideModel
});
}
//#endregion
//#region src/cron/isolated-agent/session-key.ts
function resolveCronAgentSessionKey(params) {
return toAgentStoreSessionKey({
agentId: params.agentId,
requestKey: params.sessionKey.trim(),
mainKey: params.mainKey
});
}
//#endregion
//#region src/cron/isolated-agent/skills-snapshot.ts
function resolveCronSkillsSnapshot(params) {
if (params.isFastTestEnv) return params.existingSnapshot ?? {
prompt: "",
skills: []
};
const snapshotVersion = getSkillsSnapshotVersion(params.workspaceDir);
const skillFilter = resolveAgentSkillsFilter(params.config, params.agentId);
const existingSnapshot = params.existingSnapshot;
if (!(!existingSnapshot || existingSnapshot.version !== snapshotVersion || !matchesSkillFilter(existingSnapshot.skillFilter, skillFilter))) return existingSnapshot;
return buildWorkspaceSkillSnapshot(params.workspaceDir, {
config: params.config,
skillFilter,
eligibility: { remote: getRemoteSkillEligibility() },
snapshotVersion
});
}
//#endregion
//#region src/cron/isolated-agent/run.ts
function resolveNonNegativeNumber(value) {
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : void 0;
}
function resolveCronToolPolicy(params) {
return {
requireExplicitMessageTarget: params.deliveryRequested && params.resolvedDelivery.ok,
disableMessageTool: params.deliveryContract === "cron-owned" ? true : params.deliveryRequested
};
}
async function resolveCronDeliveryContext(params) {
const deliveryPlan = resolveCronDeliveryPlan(params.job);
if (!deliveryPlan.requested) {
const resolvedDelivery = {
ok: false,
channel: void 0,
to: void 0,
accountId: void 0,
threadId: void 0,
mode: "implicit",
error: /* @__PURE__ */ new Error("cron delivery not requested")
};
return {
deliveryPlan,
deliveryRequested: false,
resolvedDelivery,
toolPolicy: resolveCronToolPolicy({
deliveryRequested: false,
resolvedDelivery,
deliveryContract: params.deliveryContract
})
};
}
const resolvedDelivery = await resolveDeliveryTarget(params.cfg, params.agentId, {
channel: deliveryPlan.channel ?? "last",
to: deliveryPlan.to,
accountId: deliveryPlan.accountId,
sessionKey: params.job.sessionKey
});
return {
deliveryPlan,
deliveryRequested: deliveryPlan.requested,
resolvedDelivery,
toolPolicy: resolveCronToolPolicy({
deliveryRequested: deliveryPlan.requested,
resolvedDelivery,
deliveryContract: params.deliveryContract
})
};
}
function appendCronDeliveryInstruction(params) {
if (!params.deliveryRequested) return params.commandBody;
return `${params.commandBody}\n\nReturn your summary as plain text; it will be delivered automatically. If the task explicitly calls for messaging a specific external recipient, note who/where it should go instead of sending it yourself.`.trim();
}
async function runCronIsolatedAgentTurn(params) {
const abortSignal = params.abortSignal ?? params.signal;
const isAborted = () => abortSignal?.aborted === true;
const abortReason = () => {
const reason = abortSignal?.reason;
return typeof reason === "string" && reason.trim() ? reason.trim() : "cron: job execution timed out";
};
const isFastTestEnv = process.env.OPENCLAW_TEST_FAST === "1";
const deliveryContract = params.deliveryContract ?? "cron-owned";
const defaultAgentId = resolveDefaultAgentId(params.cfg);
const requestedAgentId = typeof params.agentId === "string" && params.agentId.trim() ? params.agentId : typeof params.job.agentId === "string" && params.job.agentId.trim() ? params.job.agentId : void 0;
const normalizedRequested = requestedAgentId ? normalizeAgentId(requestedAgentId) : void 0;
const agentConfigOverride = normalizedRequested ? resolveAgentConfig(params.cfg, normalizedRequested) : void 0;
const agentId = normalizedRequested ?? defaultAgentId;
const agentCfg = buildCronAgentDefaultsConfig({
defaults: params.cfg.agents?.defaults,
agentConfigOverride
});
const cfgWithAgentDefaults = {
...params.cfg,
agents: Object.assign({}, params.cfg.agents, { defaults: agentCfg })
};
let catalog;
const loadCatalog = async () => {
if (!catalog) catalog = await loadModelCatalog({ config: cfgWithAgentDefaults });
return catalog;
};
const baseSessionKey = (params.sessionKey?.trim() || `cron:${params.job.id}`).trim();
const agentSessionKey = resolveCronAgentSessionKey({
sessionKey: baseSessionKey,
agentId
});
const hookExternalContentSource = (params.job.payload.kind === "agentTurn" ? params.job.payload.externalContentSource : void 0) ?? resolveHookExternalContentSource(baseSessionKey);
const workspaceDirRaw = resolveAgentWorkspaceDir(params.cfg, agentId);
const agentDir = resolveAgentDir(params.cfg, agentId);
const workspaceDir = (await ensureAgentWorkspace({
dir: workspaceDirRaw,
ensureBootstrapFiles: !agentCfg?.skipBootstrap && !isFastTestEnv
})).dir;
const isGmailHook = hookExternalContentSource === "gmail";
const now = Date.now();
const cronSession = resolveCronSession({
cfg: params.cfg,
sessionKey: agentSessionKey,
agentId,
nowMs: now,
forceNew: params.job.sessionTarget === "isolated"
});
const runSessionId = cronSession.sessionEntry.sessionId;
const runSessionKey = baseSessionKey.startsWith("cron:") ? `${agentSessionKey}:run:${runSessionId}` : agentSessionKey;
const persistSessionEntry = async () => {
if (isFastTestEnv) return;
cronSession.store[agentSessionKey] = cronSession.sessionEntry;
if (runSessionKey !== agentSessionKey) cronSession.store[runSessionKey] = cronSession.sessionEntry;
await updateSessionStore(cronSession.storePath, (store) => {
store[agentSessionKey] = cronSession.sessionEntry;
if (runSessionKey !== agentSessionKey) store[runSessionKey] = cronSession.sessionEntry;
});
};
const withRunSession = (result) => ({
...result,
sessionId: runSessionId,
sessionKey: runSessionKey
});
if (!cronSession.sessionEntry.label?.trim() && baseSessionKey.startsWith("cron:")) {
const labelSuffix = typeof params.job.name === "string" && params.job.name.trim() ? params.job.name.trim() : params.job.id;
cronSession.sessionEntry.label = `Cron: ${labelSuffix}`;
}
const resolvedModelSelection = await resolveCronModelSelection({
cfg: params.cfg,
cfgWithAgentDefaults,
agentConfigOverride,
sessionEntry: cronSession.sessionEntry,
payload: params.job.payload,
isGmailHook
});
if (!resolvedModelSelection.ok) return {
status: "error",
error: resolvedModelSelection.error
};
let provider = resolvedModelSelection.provider;
let model = resolvedModelSelection.model;
if (resolvedModelSelection.warning) logWarn(resolvedModelSelection.warning);
const hooksGmailThinking = isGmailHook ? normalizeThinkLevel(params.cfg.hooks?.gmail?.thinking) : void 0;
let thinkLevel = normalizeThinkLevel((params.job.payload.kind === "agentTurn" ? params.job.payload.thinking : void 0) ?? void 0) ?? hooksGmailThinking;
if (!thinkLevel) thinkLevel = resolveThinkingDefault({
cfg: cfgWithAgentDefaults,
provider,
model,
catalog: await loadCatalog()
});
if (thinkLevel === "xhigh" && !supportsXHighThinking(provider, model)) {
logWarn(`[cron:${params.job.id}] Thinking level "xhigh" is not supported for ${provider}/${model}; downgrading to "high".`);
thinkLevel = "high";
}
const timeoutMs = resolveAgentTimeoutMs({
cfg: cfgWithAgentDefaults,
overrideSeconds: params.job.payload.kind === "agentTurn" ? params.job.payload.timeoutSeconds : void 0
});
const agentPayload = params.job.payload.kind === "agentTurn" ? params.job.payload : null;
const { deliveryRequested, resolvedDelivery, toolPolicy } = await resolveCronDeliveryContext({
cfg: cfgWithAgentDefaults,
job: params.job,
agentId,
deliveryContract
});
const { formattedTime, timeLine } = resolveCronStyleNow(params.cfg, now);
const base = `[cron:${params.job.id} ${params.job.name}] ${params.message}`.trim();
const isExternalHook = hookExternalContentSource !== void 0 || isExternalHookSession(baseSessionKey);
const allowUnsafeExternalContent = agentPayload?.allowUnsafeExternalContent === true || isGmailHook && params.cfg.hooks?.gmail?.allowUnsafeExternalContent === true;
const shouldWrapExternal = isExternalHook && !allowUnsafeExternalContent;
let commandBody;
if (isExternalHook) {
const suspiciousPatterns = detectSuspiciousPatterns(params.message);
if (suspiciousPatterns.length > 0) logWarn(`[security] Suspicious patterns detected in external hook content (session=${baseSessionKey}, patterns=${suspiciousPatterns.length}): ${suspiciousPatterns.slice(0, 3).join(", ")}`);
}
if (shouldWrapExternal) {
const hookType = mapHookExternalContentSource(hookExternalContentSource ?? "webhook");
commandBody = `${buildSafeExternalPrompt({
content: params.message,
source: hookType,
jobName: params.job.name,
jobId: params.job.id,
timestamp: formattedTime
})}\n\n${timeLine}`.trim();
} else commandBody = `${base}\n${timeLine}`.trim();
commandBody = appendCronDeliveryInstruction({
commandBody,
deliveryRequested
});
const existingSkillsSnapshot = cronSession.sessionEntry.skillsSnapshot;
const skillsSnapshot = resolveCronSkillsSnapshot({
workspaceDir,
config: cfgWithAgentDefaults,
agentId,
existingSnapshot: existingSkillsSnapshot,
isFastTestEnv
});
if (!isFastTestEnv && skillsSnapshot !== existingSkillsSnapshot) {
cronSession.sessionEntry = {
...cronSession.sessionEntry,
updatedAt: Date.now(),
skillsSnapshot
};
await persistSessionEntry();
}
cronSession.sessionEntry.modelProvider = provider;
cronSession.sessionEntry.model = model;
cronSession.sessionEntry.systemSent = true;
try {
await persistSessionEntry();
} catch (err) {
logWarn(`[cron:${params.job.id}] Failed to persist pre-run session entry: ${String(err)}`);
}
const authProfileId = await resolveSessionAuthProfileOverride({
cfg: cfgWithAgentDefaults,
provider,
agentDir,
sessionEntry: cronSession.sessionEntry,
sessionStore: cronSession.store,
sessionKey: agentSessionKey,
storePath: cronSession.storePath,
isNewSession: cronSession.isNewSession
});
const authProfileIdSource = cronSession.sessionEntry.authProfileOverrideSource;
let runResult;
let fallbackProvider = provider;
let fallbackModel = model;
const runStartedAt = Date.now();
let runEndedAt = runStartedAt;
try {
const sessionFile = resolveSessionTranscriptPath(cronSession.sessionEntry.sessionId, agentId);
const resolvedVerboseLevel = normalizeVerboseLevel(cronSession.sessionEntry.verboseLevel) ?? normalizeVerboseLevel(agentCfg?.verboseDefault) ?? "off";
registerAgentRunContext(cronSession.sessionEntry.sessionId, {
sessionKey: agentSessionKey,
verboseLevel: resolvedVerboseLevel
});
const messageChannel = resolvedDelivery.channel;
const payloadFallbacks = params.job.payload.kind === "agentTurn" && Array.isArray(params.job.payload.fallbacks) ? params.job.payload.fallbacks : void 0;
let bootstrapPromptWarningSignaturesSeen = resolveBootstrapWarningSignaturesSeen(cronSession.sessionEntry.systemPromptReport);
const runPrompt = async (promptText) => {
const fallbackResult = await runWithModelFallback({
cfg: cfgWithAgentDefaults,
provider,
model,
runId: cronSession.sessionEntry.sessionId,
agentDir,
fallbacksOverride: payloadFallbacks ?? resolveAgentModelFallbacksOverride(params.cfg, agentId),
run: async (providerOverride, modelOverride, runOptions) => {
if (abortSignal?.aborted) throw new Error(abortReason());
const bootstrapPromptWarningSignature = bootstrapPromptWarningSignaturesSeen[bootstrapPromptWarningSignaturesSeen.length - 1];
if (isCliProvider(providerOverride, cfgWithAgentDefaults)) {
const cliSessionId = cronSession.isNewSession ? void 0 : getCliSessionId(cronSession.sessionEntry, providerOverride);
const result = await runCliAgent({
sessionId: cronSession.sessionEntry.sessionId,
sessionKey: agentSessionKey,
agentId,
sessionFile,
workspaceDir,
config: cfgWithAgentDefaults,
prompt: promptText,
provider: providerOverride,
model: modelOverride,
thinkLevel,
timeoutMs,
runId: cronSession.sessionEntry.sessionId,
cliSessionId,
bootstrapPromptWarningSignaturesSeen,
bootstrapPromptWarningSignature
});
bootstrapPromptWarningSignaturesSeen = resolveBootstrapWarningSignaturesSeen(result.meta?.systemPromptReport);
return result;
}
const result = await runEmbeddedPiAgent({
sessionId: cronSession.sessionEntry.sessionId,
sessionKey: agentSessionKey,
agentId,
trigger: "cron",
allowGatewaySubagentBinding: true,
senderIsOwner: true,
messageChannel,
agentAccountId: resolvedDelivery.accountId,
sessionFile,
agentDir,
workspaceDir,
config: cfgWithAgentDefaults,
skillsSnapshot,
prompt: promptText,
lane: resolveNestedAgentLane(params.lane),
provider: providerOverride,
model: modelOverride,
authProfileId,
authProfileIdSource,
thinkLevel,
fastMode: resolveFastModeState({
cfg: cfgWithAgentDefaults,
provider: providerOverride,
model: modelOverride,
agentId,
sessionEntry: cronSession.sessionEntry
}).enabled,
verboseLevel: resolvedVerboseLevel,
timeoutMs,
bootstrapContextMode: agentPayload?.lightContext ? "lightweight" : void 0,
bootstrapContextRunKind: "cron",
runId: cronSession.sessionEntry.sessionId,
requireExplicitMessageTarget: toolPolicy.requireExplicitMessageTarget,
disableMessageTool: toolPolicy.disableMessageTool,
allowTransientCooldownProbe: runOptions?.allowTransientCooldownProbe,
abortSignal,
bootstrapPromptWarningSignaturesSeen,
bootstrapPromptWarningSignature
});
bootstrapPromptWarningSignaturesSeen = resolveBootstrapWarningSignaturesSeen(result.meta?.systemPromptReport);
return result;
}
});
runResult = fallbackResult.result;
fallbackProvider = fallbackResult.provider;
fallbackModel = fallbackResult.model;
provider = fallbackResult.provider;
model = fallbackResult.model;
runEndedAt = Date.now();
};
await runPrompt(commandBody);
if (!runResult) throw new Error("cron isolated run returned no result");
if (!isAborted()) {
const interimRunResult = runResult;
const interimPayloads = interimRunResult.payloads ?? [];
const { deliveryPayloadHasStructuredContent: interimPayloadHasStructuredContent, outputText: interimOutputText } = resolveCronPayloadOutcome({
payloads: interimPayloads,
runLevelError: interimRunResult.meta?.error
});
const interimText = interimOutputText?.trim() ?? "";
const hasDescendantsSinceRunStart = listDescendantRunsForRequester(agentSessionKey).some((entry) => {
const descendantStartedAt = typeof entry.startedAt === "number" ? entry.startedAt : entry.createdAt;
return typeof descendantStartedAt === "number" && descendantStartedAt >= runStartedAt;
});
if (!interimRunResult.meta?.error && !interimRunResult.didSendViaMessagingTool && !interimPayloadHasStructuredContent && !interimPayloads.some((payload) => payload?.isError === true) && countActiveDescendantRuns(agentSessionKey) === 0 && !hasDescendantsSinceRunStart && isLikelyInterimCronMessage(interimText)) await runPrompt([
"Your previous response was only an acknowledgement and did not complete this cron task.",
"Complete the original task now.",
"Do not send a status update like 'on it'.",
"Use tools when needed, including sessions_spawn for parallel subtasks, wait for spawned subagents to finish, then return only the final summary."
].join(" "));
}
} catch (err) {
return withRunSession({
status: "error",
error: String(err)
});
}
if (isAborted()) return withRunSession({
status: "error",
error: abortReason()
});
if (!runResult) return withRunSession({
status: "error",
error: "cron isolated run returned no result"
});
const finalRunResult = runResult;
const payloads = finalRunResult.payloads ?? [];
let telemetry;
{
if (finalRunResult.meta?.systemPromptReport) cronSession.sessionEntry.systemPromptReport = finalRunResult.meta.systemPromptReport;
const usage = finalRunResult.meta?.agentMeta?.usage;
const promptTokens = finalRunResult.meta?.agentMeta?.promptTokens;
const modelUsed = finalRunResult.meta?.agentMeta?.model ?? fallbackModel ?? model;
const providerUsed = finalRunResult.meta?.agentMeta?.provider ?? fallbackProvider ?? provider;
const contextTokens = agentCfg?.contextTokens ?? lookupContextTokens(modelUsed, { allowAsyncLoad: false }) ?? 2e5;
setSessionRuntimeModel(cronSession.sessionEntry, {
provider: providerUsed,
model: modelUsed
});
cronSession.sessionEntry.contextTokens = contextTokens;
if (isCliProvider(providerUsed, cfgWithAgentDefaults)) {
const cliSessionId = finalRunResult.meta?.agentMeta?.sessionId?.trim();
if (cliSessionId) setCliSessionId(cronSession.sessionEntry, providerUsed, cliSessionId);
}
if (hasNonzeroUsage(usage)) {
const input = usage.input ?? 0;
const output = usage.output ?? 0;
const totalTokens = deriveSessionTotalTokens({
usage,
contextTokens,
promptTokens
});
const runEstimatedCostUsd = resolveNonNegativeNumber(estimateUsageCost({
usage,
cost: resolveModelCostConfig({
provider: providerUsed,
model: modelUsed,
config: cfgWithAgentDefaults
})
}));
cronSession.sessionEntry.inputTokens = input;
cronSession.sessionEntry.outputTokens = output;
const telemetryUsage = {
input_tokens: input,
output_tokens: output
};
if (typeof totalTokens === "number" && Number.isFinite(totalTokens) && totalTokens > 0) {
cronSession.sessionEntry.totalTokens = totalTokens;
cronSession.sessionEntry.totalTokensFresh = true;
telemetryUsage.total_tokens = totalTokens;
} else {
cronSession.sessionEntry.totalTokens = void 0;
cronSession.sessionEntry.totalTokensFresh = false;
}
cronSession.sessionEntry.cacheRead = usage.cacheRead ?? 0;
cronSession.sessionEntry.cacheWrite = usage.cacheWrite ?? 0;
if (runEstimatedCostUsd !== void 0) cronSession.sessionEntry.estimatedCostUsd = (resolveNonNegativeNumber(cronSession.sessionEntry.estimatedCostUsd) ?? 0) + runEstimatedCostUsd;
telemetry = {
model: modelUsed,
provider: providerUsed,
usage: telemetryUsage
};
} else telemetry = {
model: modelUsed,
provider: providerUsed
};
await persistSessionEntry();
}
if (isAborted()) return withRunSession({
status: "error",
error: abortReason(),
...telemetry
});
let { summary, outputText, synthesizedText, deliveryPayloads, deliveryPayloadHasStructuredContent, hasFatalErrorPayload, embeddedRunError } = resolveCronPayloadOutcome({
payloads,
runLevelError: finalRunResult.meta?.error
});
const deliveryBestEffort = resolveCronDeliveryBestEffort(params.job);
const resolveRunOutcome = (params) => withRunSession({
status: hasFatalErrorPayload ? "error" : "ok",
...hasFatalErrorPayload ? { error: embeddedRunError ?? "cron isolated run returned an error payload" } : {},
summary,
outputText,
delivered: params?.delivered,
deliveryAttempted: params?.deliveryAttempted,
...telemetry
});
const ackMaxChars = resolveHeartbeatAckMaxChars(agentCfg);
const skipHeartbeatDelivery = deliveryRequested && isHeartbeatOnlyResponse(payloads, ackMaxChars);
const skipMessagingToolDelivery = deliveryContract === "shared" && deliveryRequested && finalRunResult.didSendViaMessagingTool === true && (finalRunResult.messagingToolSentTargets ?? []).some((target) => matchesMessagingToolDeliveryTarget(target, {
channel: resolvedDelivery.channel,
to: resolvedDelivery.to,
accountId: resolvedDelivery.accountId
}));
const deliveryResult = await dispatchCronDelivery({
cfg: params.cfg,
cfgWithAgentDefaults,
deps: params.deps,
job: params.job,
agentId,
agentSessionKey,
runSessionId,
runStartedAt,
runEndedAt,
timeoutMs,
resolvedDelivery,
deliveryRequested,
skipHeartbeatDelivery,
skipMessagingToolDelivery,
deliveryBestEffort,
deliveryPayloadHasStructuredContent,
deliveryPayloads,
synthesizedText,
summary,
outputText,
telemetry,
abortSignal,
isAborted,
abortReason,
withRunSession
});
if (deliveryResult.result) {
const resultWithDeliveryMeta = {
...deliveryResult.result,
deliveryAttempted: deliveryResult.result.deliveryAttempted ?? deliveryResult.deliveryAttempted
};
if (!hasFatalErrorPayload || deliveryResult.result.status !== "ok") return resultWithDeliveryMeta;
return resolveRunOutcome({
delivered: deliveryResult.result.delivered,
deliveryAttempted: resultWithDeliveryMeta.deliveryAttempted
});
}
const delivered = deliveryResult.delivered;
const deliveryAttempted = deliveryResult.deliveryAttempted;
summary = deliveryResult.summary;
outputText = deliveryResult.outputText;
return resolveRunOutcome({
delivered,
deliveryAttempted
});
}
//#endregion
//#region src/cron/run-log.ts
function assertSafeCronRunLogJobId(jobId) {
const trimmed = jobId.trim();
if (!trimmed) throw new Error("invalid cron run log job id");
if (trimmed.includes("/") || trimmed.includes("\\") || trimmed.includes("\0")) throw new Error("invalid cron run log job id");
return trimmed;
}
function resolveCronRunLogPath(params) {
const storePath = path.resolve(params.storePath);
const dir = path.dirname(storePath);
const runsDir = path.resolve(dir, "runs");
const safeJobId = assertSafeCronRunLogJobId(params.jobId);
const resolvedPath = path.resolve(runsDir, `${safeJobId}.jsonl`);
if (!resolvedPath.startsWith(`${runsDir}${path.sep}`)) throw new Error("invalid cron run log job id");
return resolvedPath;
}
const writesByPath = /* @__PURE__ */ new Map();
async function setSecureFileMode(filePath) {
await fs$1.chmod(filePath, 384).catch(() => void 0);
}
const DEFAULT_CRON_RUN_LOG_MAX_BYTES = 2e6;
const DEFAULT_CRON_RUN_LOG_KEEP_LINES = 2e3;
function resolveCronRunLogPruneOptions(cfg) {
let maxBytes = DEFAULT_CRON_RUN_LOG_MAX_BYTES;
if (cfg?.maxBytes !== void 0) try {
maxBytes = parseByteSize(String(cfg.maxBytes).trim(), { defaultUnit: "b" });
} catch {
maxBytes = DEFAULT_CRON_RUN_LOG_MAX_BYTES;
}
let keepLines = DEFAULT_CRON_RUN_LOG_KEEP_LINES;
if (typeof cfg?.keepLines === "number" && Number.isFinite(cfg.keepLines) && cfg.keepLines > 0) keepLines = Math.floor(cfg.keepLines);
return {
maxBytes,
keepLines
};
}
async function drainPendingWrite(filePath) {
const resolved = path.resolve(filePath);
const pending = writesByPath.get(resolved);
if (pending) await pending.catch(() => void 0);
}
async function pruneIfNeeded(filePath, opts) {
const stat = await fs$1.stat(filePath).catch(() => null);
if (!stat || stat.size <= opts.maxBytes) return;
const lines = (await fs$1.readFile(filePath, "utf-8").catch(() => "")).split("\n").map((l) => l.trim()).filter(Boolean);
const kept = lines.slice(Math.max(0, lines.length - opts.keepLines));
const { randomBytes } = await import("node:crypto");
const tmp = `${filePath}.${process.pid}.${randomBytes(8).toString("hex")}.tmp`;
await fs$1.writeFile(tmp, `${kept.join("\n")}\n`, {
encoding: "utf-8",
mode: 384
});
await setSecureFileMode(tmp);
await fs$1.rename(tmp, filePath);
await setSecureFileMode(filePath);
}
async function appendCronRunLog(filePath, entry, opts) {
const resolved = path.resolve(filePath);
const next = (writesByPath.get(resolved) ?? Promise.resolve()).catch(() => void 0).then(async () => {
const runDir = path.dirname(resolved);
await fs$1.mkdir(runDir, {
recursive: true,
mode: 448
});
await fs$1.chmod(runDir, 448).catch(() => void 0);
await fs$1.appendFile(resolved, `${JSON.stringify(entry)}\n`, {
encoding: "utf-8",
mode: 384
});
await setSecureFileMode(resolved);
await pruneIfNeeded(resolved, {
maxBytes: opts?.maxBytes ?? 2e6,
keepLines: opts?.keepLines ?? 2e3
});
});
writesByPath.set(resolved, next);
try {
await next;
} finally {
if (writesByPath.get(resolved) === next) writesByPath.delete(resolved);
}
}
function normalizeRunStatusFilter(status) {
if (status === "ok" || status === "error" || status === "skipped" || status === "all") return status;
return "all";
}
function normalizeRunStatuses(opts) {
if (Array.isArray(opts?.statuses) && opts.statuses.length > 0) {
const filtered = opts.statuses.filter((status) => status === "ok" || status === "error" || status === "skipped");
if (filtered.length > 0) return Array.from(new Set(filtered));
}
const status = normalizeRunStatusFilter(opts?.status);
if (status === "all") return null;
return [status];
}
function normalizeDeliveryStatuses(opts) {
if (Array.isArray(opts?.deliveryStatuses) && opts.deliveryStatuses.length > 0) {
const filtered = opts.deliveryStatuses.filter((status) => status === "delivered" || status === "not-delivered" || status === "unknown" || status === "not-requested");
if (filtered.length > 0) return Array.from(new Set(filtered));
}
if (opts?.deliveryStatus === "delivered" || opts?.deliveryStatus === "not-delivered" || opts?.deliveryStatus === "unknown" || opts?.deliveryStatus === "not-requested") return [opts.deliveryStatus];
return null;
}
function parseAllRunLogEntries(raw, opts) {
const jobId = opts?.jobId?.trim() || void 0;
if (!raw.trim()) return [];
const parsed = [];
const lines = raw.split("\n");
for (let i = 0; i < lines.length; i++) {
const line = lines[i]?.trim();
if (!line) continue;
try {
const obj = JSON.parse(line);
if (!obj || typeof obj !== "object") continue;
if (obj.action !== "finished") continue;
if (typeof obj.jobId !== "string" || obj.jobId.trim().length === 0) continue;
if (typeof obj.ts !== "number" || !Number.isFinite(obj.ts)) continue;
if (jobId && obj.jobId !== jobId) continue;
const usage = obj.usage && typeof obj.usage === "object" ? obj.usage : void 0;
const entry = {
ts: obj.ts,
jobId: obj.jobId,
action: "finished",
status: obj.status,
error: obj.error,
summary: obj.summary,
runAtMs: obj.runAtMs,
durationMs: obj.durationMs,
nextRunAtMs: obj.nextRunAtMs,
model: typeof obj.model === "string" && obj.model.trim() ? obj.model : void 0,
provider: typeof obj.provider === "string" && obj.provider.trim() ? obj.provider : void 0,
usage: usage ? {
input_tokens: typeof usage.input_tokens === "number" ? usage.input_tokens : void 0,
output_tokens: typeof usage.output_tokens === "number" ? usage.output_tokens : void 0,
total_tokens: typeof usage.total_tokens === "number" ? usage.total_tokens : void 0,
cache_read_tokens: typeof usage.cache_read_tokens === "number" ? usage.cache_read_tokens : void 0,
cache_write_tokens: typeof usage.cache_write_tokens === "number" ? usage.cache_write_tokens : void 0
} : void 0
};
if (typeof obj.delivered === "boolean") entry.delivered = obj.delivered;
if (obj.deliveryStatus === "delivered" || obj.deliveryStatus === "not-delivered" || obj.deliveryStatus === "unknown" || obj.deliveryStatus === "not-requested") entry.deliveryStatus = obj.deliveryStatus;
if (typeof obj.deliveryError === "string") entry.deliveryError = obj.deliveryError;
if (typeof obj.sessionId === "string" && obj.sessionId.trim().length > 0) entry.sessionId = obj.sessionId;
if (typeof obj.sessionKey === "string" && obj.sessionKey.trim().length > 0) entry.sessionKey = obj.sessionKey;
parsed.push(entry);
} catch {}
}
return parsed;
}
function filterRunLogEntries(entries, opts) {
return entries.filter((entry) => {
if (opts.statuses && (!entry.status || !opts.statuses.includes(entry.status))) return false;
if (opts.deliveryStatuses) {
const deliveryStatus = entry.deliveryStatus ?? "not-requested";
if (!opts.deliveryStatuses.includes(deliveryStatus)) return false;
}
if (!opts.query) return true;
return opts.queryTextForEntry(entry).toLowerCase().includes(opts.query);
});
}
async function readCronRunLogEntriesPage(filePath, opts) {
await drainPendingWrite(filePath);
const limit = Math.max(1, Math.min(200, Math.floor(opts?.limit ?? 50)));
const raw = await fs$1.readFile(path.resolve(filePath), "utf-8").catch(() => "");
const statuses = normalizeRunStatuses(opts);
const deliveryStatuses = normalizeDeliveryStatuses(opts);
const query = opts?.query?.trim().toLowerCase() ?? "";
const sortDir = opts?.sortDir === "asc" ? "asc" : "desc";
const filtered = filterRunLogEntries(parseAllRunLogEntries(raw, { jobId: opts?.jobId }), {
statuses,
deliveryStatuses,
query,
queryTextForEntry: (entry) => [
entry.summary ?? "",
entry.error ?? "",
entry.jobId
].join(" ")
});
const sorted = sortDir === "asc" ? filtered.toSorted((a, b) => a.ts - b.ts) : filtered.toSorted((a, b) => b.ts - a.ts);
const total = sorted.length;
const offset = Math.max(0, Math.min(total, Math.floor(opts?.offset ?? 0)));
const entries = sorted.slice(offset, offset + limit);
const nextOffset = offset + entries.length;
return {
entries,
total,
offset,
limit,
hasMore: nextOffset < total,
nextOffset: nextOffset < total ? nextOffset : null
};
}
async function readCronRunLogEntriesPageAll(opts) {
const limit = Math.max(1, Math.min(200, Math.floor(opts.limit ?? 50)));
const statuses = normalizeRunStatuses(opts);
const deliveryStatuses = normalizeDeliveryStatuses(opts);
const query = opts.query?.trim().toLowerCase() ?? "";
const sortDir = opts.sortDir === "asc" ? "asc" : "desc";
const runsDir = path.resolve(path.dirname(path.resolve(opts.storePath)), "runs");
const jsonlFiles = (await fs$1.readdir(runsDir, { withFileTypes: true }).catch(() => [])).filter((entry) => entry.isFile() && entry.name.endsWith(".jsonl")).map((entry) => path.join(runsDir, entry.name));
if (jsonlFiles.length === 0) return {
entries: [],
total: 0,
offset: 0,
limit,
hasMore: false,
nextOffset: null
};
await Promise.all(jsonlFiles.map((f) => drainPendingWrite(f)));
const filtered = filterRunLogEntries((await Promise.all(jsonlFiles.map(async (filePath) => {
return parseAllRunLogEntries(await fs$1.readFile(filePath, "utf-8").catch(() => ""));
}))).flat(), {
statuses,
deliveryStatuses,
query,
queryTextForEntry: (entry) => {
const jobName = opts.jobNameById?.[entry.jobId] ?? "";
return [
entry.summary ?? "",
entry.error ?? "",
entry.jobId,
jobName
].join(" ");
}
});
const sorted = sortDir === "asc" ? filtered.toSorted((a, b) => a.ts - b.ts) : filtered.toSorted((a, b) => b.ts - a.ts);
const total = sorted.length;
const offset = Math.max(0, Math.min(total, Math.floor(opts.offset ?? 0)));
const entries = sorted.slice(offset, offset + limit);
if (opts.jobNameById) for (const entry of entries) {
const jobName = opts.jobNameById[entry.jobId];
if (jobName) entry.jobName = jobName;
}
const nextOffset = offset + entries.length;
return {
entries,
total,
offset,
limit,
hasMore: nextOffset < total,
nextOffset: nextOffset < total ? nextOffset : null
};
}
//#endregion
//#region src/cron/service/initial-delivery.ts
function normalizeCronCreateDeliveryInput(input) {
const payloadRecord = input.payload && typeof input.payload === "object" ? { ...input.payload } : null;
const normalizedLegacy = normalizeLegacyDeliveryInput({
delivery: input.delivery && typeof input.delivery === "object" ? { ...input.delivery } : null,
payload: payloadRecord
});
if (!normalizedLegacy.mutated) return input;
return {
...input,
payload: payloadRecord ? payloadRecord : input.payload,
delivery: normalizedLegacy.delivery ?? input.delivery
};
}
function resolveInitialCronDelivery(input) {
if (input.delivery) return input.delivery;
if (input.sessionTarget === "isolated" && input.payload.kind === "agentTurn") return { mode: "announce" };
}
//#endregion
//#region src/cron/service/jobs.ts
const STUCK_RUN_MS = 7200 * 1e3;
const STAGGER_OFFSET_CACHE_MAX = 4096;
const staggerOffsetCache = /* @__PURE__ */ new Map();
function isFiniteTimestamp(value) {
return typeof value === "number" && Number.isFinite(value);
}
function resolveStableCronOffsetMs(jobId, staggerMs) {
if (staggerMs <= 1) return 0;
const cacheKey = `${staggerMs}:${jobId}`;
const cached = staggerOffsetCache.get(cacheKey);
if (cached !== void 0) return cached;
const offset = crypto.createHash("sha256").update(jobId).digest().readUInt32BE(0) % staggerMs;
if (staggerOffsetCache.size >= STAGGER_OFFSET_CACHE_MAX) {
const first = staggerOffsetCache.keys().next();
if (!first.done) staggerOffsetCache.delete(first.value);
}
staggerOffsetCache.set(cacheKey, offset);
return offset;
}
function computeStaggeredCronNextRunAtMs(job, nowMs) {
if (job.schedule.kind !== "cron") return computeNextRunAtMs(job.schedule, nowMs);
const staggerMs = resolveCronStaggerMs(job.schedule);
const offsetMs = resolveStableCronOffsetMs(job.id, staggerMs);
if (offsetMs <= 0) return computeNextRunAtMs(job.schedule, nowMs);
let cursorMs = Math.max(0, nowMs - offsetMs);
for (let attempt = 0; attempt < 4; attempt += 1) {
const baseNext = computeNextRunAtMs(job.schedule, cursorMs);
if (baseNext === void 0) return;
const shifted = baseNext + offsetMs;
if (shifted > nowMs) return shifted;
cursorMs = Math.max(cursorMs + 1, baseNext + 1e3);
}
}
function computeStaggeredCronPreviousRunAtMs(job, nowMs) {
if (job.schedule.kind !== "cron") return;
const staggerMs = resolveCronStaggerMs(job.schedule);
const offsetMs = resolveStableCronOffsetMs(job.id, staggerMs);
if (offsetMs <= 0) return computePreviousRunAtMs(job.schedule, nowMs);
let cursorMs = Math.max(0, nowMs - offsetMs);
for (let attempt = 0; attempt < 4; attempt += 1) {
const basePrevious = computePreviousRunAtMs(job.schedule, cursorMs);
if (basePrevious === void 0) return;
const shifted = basePrevious + offsetMs;
if (shifted <= nowMs) return shifted;
cursorMs = Math.max(0, basePrevious - 1e3);
}
}
function resolveEveryAnchorMs(params) {
const coerced = coerceFiniteScheduleNumber(params.schedule.anchorMs);
if (coerced !== void 0) return Math.max(0, Math.floor(coerced));
if (isFiniteTimestamp(params.fallbackAnchorMs)) return Math.max(0, Math.floor(params.fallbackAnchorMs));
return 0;
}
function assertSupportedJobSpec(job) {
const isIsolatedLike = job.sessionTarget === "isolated" || job.sessionTarget === "current" || job.sessionTarget.startsWith("session:");
if (job.sessionTarget === "main" && job.payload.kind !== "systemEvent") throw new Error("main cron jobs require payload.kind=\"systemEvent\"");
if (isIsolatedLike && job.payload.kind !== "agentTurn") throw new Error("isolated/current/session cron jobs require payload.kind=\"agentTurn\"");
}
function assertMainSessionAgentId(job, defaultAgentId) {
if (job.sessionTarget !== "main") return;
if (!job.agentId) return;
if (normalizeAgentId(job.agentId) !== normalizeAgentId(defaultAgentId)) throw new Error(`cron: sessionTarget "main" is only valid for the default agent. Use sessionTarget "isolated" with payload.kind "agentTurn" for non-default agents (agentId: ${job.agentId})`);
}
const TELEGRAM_TME_URL_REGEX = /^https?:\/\/t\.me\/|t\.me\//i;
const TELEGRAM_SLASH_TOPIC_REGEX = /^-?\d+\/\d+$/;
function validateTelegramDeliveryTarget(to) {
if (!to) return;
const trimmed = to.trim();
if (TELEGRAM_TME_URL_REGEX.test(trimmed)) return;
if (TELEGRAM_SLASH_TOPIC_REGEX.test(trimmed)) return `Invalid Telegram delivery target "${to}". Use colon (:) as delimiter for topics, not slash. Valid formats: -1001234567890, -1001234567890:123, -1001234567890:topic:123, @username, https://t.me/username`;
}
function assertDeliverySupport(job) {
if (!job.delivery || job.delivery.mode === "none") return;
if (job.delivery.mode === "webhook") {
const target = normalizeHttpWebhookUrl(job.delivery.to);
if (!target) throw new Error("cron webhook delivery requires delivery.to to be a valid http(s) URL");
job.delivery.to = target;
return;
}
if (!(job.sessionTarget === "isolated" || job.sessionTarget === "current" || job.sessionTarget.startsWith("session:"))) throw new Error("cron channel delivery config is only supported for sessionTarget=\"isolated\"");
if (job.delivery.channel === "telegram") {
const telegramError = validateTelegramDeliveryTarget(job.delivery.to);
if (telegramError) throw new Error(telegramError);
}
}
function assertFailureDestinationSupport(job) {
const failureDestination = job.delivery?.failureDestination;
if (!failureDestination) return;
if (job.sessionTarget === "main" && job.delivery?.mode !== "webhook") throw new Error("cron delivery.failureDestination is only supported for sessionTarget=\"isolated\" unless delivery.mode=\"webhook\"");
if (failureDestination.mode === "webhook") {
const target = normalizeHttpWebhookUrl(failureDestination.to);
if (!target) throw new Error("cron failure destination webhook requires delivery.failureDestination.to to be a valid http(s) URL");
failureDestination.to = target;
}
}
function findJobOrThrow(state, id) {
const job = state.store?.jobs.find((j) => j.id === id);
if (!job) throw new Error(`unknown cron job id: ${id}`);
return job;
}
function computeJobNextRunAtMs(job, nowMs) {
if (!job.enabled) return;
if (job.schedule.kind === "every") {
const everyMsRaw = coerceFiniteScheduleNumber(job.schedule.everyMs);
if (everyMsRaw === void 0) return;
const everyMs = Math.max(1, Math.floor(everyMsRaw));
const lastRunAtMs = job.state.lastRunAtMs;
if (typeof lastRunAtMs === "number" && Number.isFinite(lastRunAtMs)) {
const nextFromLastRun = Math.floor(lastRunAtMs) + everyMs;
if (nextFromLastRun > nowMs) return nextFromLastRun;
}
const fallbackAnchorMs = isFiniteTimestamp(job.createdAtMs) ? job.createdAtMs : nowMs;
const anchorMs = resolveEveryAnchorMs({
schedule: job.schedule,
fallbackAnchorMs
});
const next = computeNextRunAtMs({
...job.schedule,
everyMs,
anchorMs
}, nowMs);
return isFiniteTimestamp(next) ? next : void 0;
}
if (job.schedule.kind === "at") {
const schedule = job.schedule;
const atMs = typeof schedule.atMs === "number" && Number.isFinite(schedule.atMs) && schedule.atMs > 0 ? schedule.atMs : typeof schedule.atMs === "string" ? parseAbsoluteTimeMs(schedule.atMs) : typeof schedule.at === "string" ? parseAbsoluteTimeMs(schedule.at) : null;
if (job.state.lastStatus === "ok" && job.state.lastRunAtMs) {
if (atMs !== null && Number.isFinite(atMs) && atMs > job.state.lastRunAtMs) return atMs;
return;
}
return atMs !== null && Number.isFinite(atMs) ? atMs : void 0;
}
const next = computeStaggeredCronNextRunAtMs(job, nowMs);
if (next === void 0 && job.schedule.kind === "cron") return computeStaggeredCronNextRunAtMs(job, Math.floor(nowMs / 1e3) * 1e3 + 1e3);
return isFiniteTimestamp(next) ? next : void 0;
}
function computeJobPreviousRunAtMs(job, nowMs) {
if (!job.enabled || job.schedule.kind !== "cron") return;
const previous = computeStaggeredCronPreviousRunAtMs(job, nowMs);
return isFiniteTimestamp(previous) ? previous : void 0;
}
/** Maximum consecutive schedule errors before auto-disabling a job. */
const MAX_SCHEDULE_ERRORS = 3;
function recordScheduleComputeError(params) {
const { state, job, err } = params;
const errorCount = (job.state.scheduleErrorCount ?? 0) + 1;
const errText = String(err);
job.state.scheduleErrorCount = errorCount;
job.state.nextRunAtMs = void 0;
job.state.lastError = `schedule error: ${errText}`;
if (errorCount >= MAX_SCHEDULE_ERRORS) {
job.enabled = false;
state.deps.log.error({
jobId: job.id,
name: job.name,
errorCount,
err: errText
}, "cron: auto-disabled job after repeated schedule errors");
const notifyText = `⚠️ Cron job "${job.name}" has been auto-disabled after ${errorCount} consecutive schedule errors. Last error: ${errText}`;
state.deps.enqueueSystemEvent(notifyText, {
agentId: job.agentId,
sessionKey: job.sessionKey,
contextKey: `cron:${job.id}:auto-disabled`
});
state.deps.requestHeartbeatNow({
reason: `cron:${job.id}:auto-disabled`,
agentId: job.agentId,
sessionKey: job.sessionKey
});
} else state.deps.log.warn({
jobId: job.id,
name: job.name,
errorCount,
err: errText
}, "cron: failed to compute next run for job (skipping)");
return true;
}
function normalizeJobTickState(params) {
const { state, job, nowMs } = params;
let changed = false;
if (!job.state) {
job.state = {};
changed = true;
}
if (!job.enabled) {
if (job.state.nextRunAtMs !== void 0) {
job.state.nextRunAtMs = void 0;
changed = true;
}
if (job.state.runningAtMs !== void 0) {
job.state.runningAtMs = void 0;
changed = true;
}
return {
changed,
skip: true
};
}
if (!isFiniteTimestamp(job.state.nextRunAtMs) && job.state.nextRunAtMs !== void 0) {
job.state.nextRunAtMs = void 0;
changed = true;
}
const runningAt = job.state.runningAtMs;
if (typeof runningAt === "number" && nowMs - runningAt > STUCK_RUN_MS) {
state.deps.log.warn({
jobId: job.id,
runningAtMs: runningAt
}, "cron: clearing stuck running marker");
job.state.runningAtMs = void 0;
changed = true;
}
return {
changed,
skip: false
};
}
function walkSchedulableJobs(state, fn, nowMs = state.deps.nowMs()) {
if (!state.store) return false;
let changed = false;
for (const job of state.store.jobs) {
const tick = normalizeJobTickState({
state,
job,
nowMs
});
if (tick.changed) changed = true;
if (tick.skip) continue;
if (fn({
job,
nowMs
})) changed = true;
}
return changed;
}
function recomputeJobNextRunAtMs(params) {
let changed = false;
try {
const newNext = computeJobNextRunAtMs(params.job, params.nowMs);
if (params.job.state.nextRunAtMs !== newNext) {
params.job.state.nextRunAtMs = newNext;
changed = true;
}
if (params.job.state.scheduleErrorCount) {
params.job.state.scheduleErrorCount = void 0;
changed = true;
}
} catch (err) {
if (recordScheduleComputeError({
state: params.state,
job: params.job,
err
})) changed = true;
}
return changed;
}
function recomputeNextRuns(state) {
return walkSchedulableJobs(state, ({ job, nowMs: now }) => {
let changed = false;
const nextRun = job.state.nextRunAtMs;
if (!isFiniteTimestamp(nextRun) || now >= nextRun) {
if (recomputeJobNextRunAtMs({
state,
job,
nowMs: now
})) changed = true;
}
return changed;
});
}
/**
* Maintenance-only version of recomputeNextRuns that handles disabled jobs
* and stuck markers, but does NOT recompute nextRunAtMs for enabled jobs
* with existing values. Used during timer ticks when no due jobs were found
* to prevent silently advancing past-due nextRunAtMs values without execution
* (see #13992).
*/
function recomputeNextRunsForMaintenance(state, opts) {
const recomputeExpired = opts?.recomputeExpired ?? false;
return walkSchedulableJobs(state, ({ job, nowMs: now }) => {
let changed = false;
if (!isFiniteTimestamp(job.state.nextRunAtMs)) {
if (recomputeJobNextRunAtMs({
state,
job,
nowMs: now
})) changed = true;
} else if (recomputeExpired && now >= job.state.nextRunAtMs && typeof job.state.runningAtMs !== "number") {
const lastRun = job.state.lastRunAtMs;
if (isFiniteTimestamp(lastRun) && lastRun >= job.state.nextRunAtMs) {
if (recomputeJobNextRunAtMs({
state,
job,
nowMs: now
})) changed = true;
}
}
return changed;
}, opts?.nowMs);
}
function nextWakeAtMs(state) {
const enabled = (state.store?.jobs ?? []).filter((j) => j.enabled && isFiniteTimestamp(j.state.nextRunAtMs));
if (enabled.length === 0) return;
const first = enabled[0]?.state.nextRunAtMs;
if (!isFiniteTimestamp(first)) return;
return enabled.reduce((min, j) => {
const next = j.state.nextRunAtMs;
return isFiniteTimestamp(next) ? Math.min(min, next) : min;
}, first);
}
function createJob(state, input) {
const now = state.deps.nowMs();
const id = crypto.randomUUID();
const schedule = input.schedule.kind === "every" ? {
...input.schedule,
anchorMs: resolveEveryAnchorMs({
schedule: input.schedule,
fallbackAnchorMs: now
})
} : input.schedule.kind === "cron" ? (() => {
const explicitStaggerMs = normalizeCronStaggerMs(input.schedule.staggerMs);
if (explicitStaggerMs !== void 0) return {
...input.schedule,
staggerMs: explicitStaggerMs
};
const defaultStaggerMs = resolveDefaultCronStaggerMs(input.schedule.expr);
return defaultStaggerMs !== void 0 ? {
...input.schedule,
staggerMs: defaultStaggerMs
} : input.schedule;
})() : input.schedule;
const deleteAfterRun = typeof input.deleteAfterRun === "boolean" ? input.deleteAfterRun : schedule.kind === "at" ? true : void 0;
const enabled = typeof input.enabled === "boolean" ? input.enabled : true;
const job = {
id,
agentId: normalizeOptionalAgentId(input.agentId),
sessionKey: normalizeOptionalSessionKey(input.sessionKey),
name: normalizeRequiredName(input.name),
description: normalizeOptionalText$1(input.description),
enabled,
deleteAfterRun,
createdAtMs: now,
updatedAtMs: now,
schedule,
sessionTarget: input.sessionTarget,
wakeMode: input.wakeMode,
payload: input.payload,
delivery: resolveInitialCronDelivery(input),
failureAlert: input.failureAlert,
state: { ...input.state }
};
assertSupportedJobSpec(job);
assertMainSessionAgentId(job, state.deps.defaultAgentId);
assertDeliverySupport(job);
assertFailureDestinationSupport(job);
job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
return job;
}
function applyJobPatch(job, patch, opts) {
if ("name" in patch) job.name = normalizeRequiredName(patch.name);
if ("description" in patch) job.description = normalizeOptionalText$1(patch.description);
if (typeof patch.enabled === "boolean") job.enabled = patch.enabled;
if (typeof patch.deleteAfterRun === "boolean") job.deleteAfterRun = patch.deleteAfterRun;
if (patch.schedule) if (patch.schedule.kind === "cron") {
const explicitStaggerMs = normalizeCronStaggerMs(patch.schedule.staggerMs);
if (explicitStaggerMs !== void 0) job.schedule = {
...patch.schedule,
staggerMs: explicitStaggerMs
};
else if (job.schedule.kind === "cron") job.schedule = {
...patch.schedule,
staggerMs: job.schedule.staggerMs
};
else {
const defaultStaggerMs = resolveDefaultCronStaggerMs(patch.schedule.expr);
job.schedule = defaultStaggerMs !== void 0 ? {
...patch.schedule,
staggerMs: defaultStaggerMs
} : patch.schedule;
}
} else job.schedule = patch.schedule;
if (patch.sessionTarget) job.sessionTarget = patch.sessionTarget;
if (patch.wakeMode) job.wakeMode = patch.wakeMode;
if (patch.payload) job.payload = mergeCronPayload(job.payload, patch.payload);
if (!patch.delivery && patch.payload?.kind === "agentTurn") {
const legacyDeliveryPatch = buildLegacyDeliveryPatch(patch.payload);
const isIsolatedLike = job.sessionTarget === "isolated" || job.sessionTarget === "current" || job.sessionTarget.startsWith("session:");
if (legacyDeliveryPatch && isIsolatedLike && job.payload.kind === "agentTurn") job.delivery = mergeCronDelivery(job.delivery, legacyDeliveryPatch);
}
if (patch.delivery) job.delivery = mergeCronDelivery(job.delivery, patch.delivery);
if ("failureAlert" in patch) job.failureAlert = mergeCronFailureAlert(job.failureAlert, patch.failureAlert);
if (job.sessionTarget === "main" && job.delivery?.mode !== "webhook" && job.delivery?.failureDestination) throw new Error("cron delivery.failureDestination is only supported for sessionTarget=\"isolated\" unless delivery.mode=\"webhook\"");
if (job.sessionTarget === "main" && job.delivery?.mode !== "webhook") job.delivery = void 0;
if (patch.state) job.state = {
...job.state,
...patch.state
};
if ("agentId" in patch) job.agentId = normalizeOptionalAgentId(patch.agentId);
if ("sessionKey" in patch) job.sessionKey = normalizeOptionalSessionKey(patch.sessionKey);
assertSupportedJobSpec(job);
assertMainSessionAgentId(job, opts?.defaultAgentId);
assertDeliverySupport(job);
assertFailureDestinationSupport(job);
}
function mergeCronPayload(existing, patch) {
if (patch.kind !== existing.kind) return buildPayloadFromPatch(patch);
if (patch.kind === "systemEvent") {
if (existing.kind !== "systemEvent") return buildPayloadFromPatch(patch);
return {
kind: "systemEvent",
text: typeof patch.text === "string" ? patch.text : existing.text
};
}
if (existing.kind !== "agentTurn") return buildPayloadFromPatch(patch);
const next = { ...existing };
if (typeof patch.message === "string") next.message = patch.message;
if (typeof patch.model === "string") next.model = patch.model;
if (typeof patch.thinking === "string") next.thinking = patch.thinking;
if (typeof patch.timeoutSeconds === "number") next.timeoutSeconds = patch.timeoutSeconds;
if (typeof patch.lightContext === "boolean") next.lightContext = patch.lightContext;
if (typeof patch.allowUnsafeExternalContent === "boolean") next.allowUnsafeExternalContent = patch.allowUnsafeExternalContent;
if (typeof patch.deliver === "boolean") next.deliver = patch.deliver;
if (typeof patch.channel === "string") next.channel = patch.channel;
if (typeof patch.to === "string") next.to = patch.to;
if (typeof patch.bestEffortDeliver === "boolean") next.bestEffortDeliver = patch.bestEffortDeliver;
return next;
}
function buildLegacyDeliveryPatch(payload) {
const deliver = payload.deliver;
const toRaw = typeof payload.to === "string" ? payload.to.trim() : "";
if (!(typeof deliver === "boolean" || typeof payload.bestEffortDeliver === "boolean" || Boolean(toRaw))) return null;
const patch = {};
let hasPatch = false;
if (deliver === false) {
patch.mode = "none";
hasPatch = true;
} else if (deliver === true || toRaw) {
patch.mode = "announce";
hasPatch = true;
}
if (typeof payload.channel === "string") {
const channel = payload.channel.trim().toLowerCase();
patch.channel = channel ? channel : void 0;
hasPatch = true;
}
if (typeof payload.to === "string") {
patch.to = payload.to.trim();
hasPatch = true;
}
if (typeof payload.bestEffortDeliver === "boolean") {
patch.bestEffort = payload.bestEffortDeliver;
hasPatch = true;
}
return hasPatch ? patch : null;
}
function buildPayloadFromPatch(patch) {
if (patch.kind === "systemEvent") {
if (typeof patch.text !== "string" || patch.text.length === 0) throw new Error("cron.update payload.kind=\"systemEvent\" requires text");
return {
kind: "systemEvent",
text: patch.text
};
}
if (typeof patch.message !== "string" || patch.message.length === 0) throw new Error("cron.update payload.kind=\"agentTurn\" requires message");
return {
kind: "agentTurn",
message: patch.message,
model: patch.model,
thinking: patch.thinking,
timeoutSeconds: patch.timeoutSeconds,
lightContext: patch.lightContext,
allowUnsafeExternalContent: patch.allowUnsafeExternalContent,
deliver: patch.deliver,
channel: patch.channel,
to: patch.to,
bestEffortDeliver: patch.bestEffortDeliver
};
}
function normalizeOptionalTrimmedString(value) {
const trimmed = typeof value === "string" ? value.trim() : "";
return trimmed ? trimmed : void 0;
}
function mergeCronDelivery(existing, patch) {
const next = {
mode: existing?.mode ?? "none",
channel: existing?.channel,
to: existing?.to,
accountId: existing?.accountId,
bestEffort: existing?.bestEffort,
failureDestination: existing?.failureDestination
};
if (typeof patch.mode === "string") next.mode = patch.mode === "deliver" ? "announce" : patch.mode;
if ("channel" in patch) next.channel = normalizeOptionalTrimmedString(patch.channel);
if ("to" in patch) next.to = normalizeOptionalTrimmedString(patch.to);
if ("accountId" in patch) next.accountId = normalizeOptionalTrimmedString(patch.accountId);
if (typeof patch.bestEffort === "boolean") next.bestEffort = patch.bestEffort;
if ("failureDestination" in patch) if (patch.failureDestination === void 0) next.failureDestination = void 0;
else {
const existingFd = next.failureDestination;
const patchFd = patch.failureDestination;
const nextFd = {
channel: existingFd?.channel,
to: existingFd?.to,
accountId: existingFd?.accountId,
mode: existingFd?.mode
};
if (patchFd) {
if ("channel" in patchFd) {
const channel = typeof patchFd.channel === "string" ? patchFd.channel.trim() : "";
nextFd.channel = channel ? channel : void 0;
}
if ("to" in patchFd) {
const to = typeof patchFd.to === "string" ? patchFd.to.trim() : "";
nextFd.to = to ? to : void 0;
}
if ("accountId" in patchFd) {
const accountId = typeof patchFd.accountId === "string" ? patchFd.accountId.trim() : "";
nextFd.accountId = accountId ? accountId : void 0;
}
if ("mode" in patchFd) {
const mode = typeof patchFd.mode === "string" ? patchFd.mode.trim() : "";
nextFd.mode = mode === "announce" || mode === "webhook" ? mode : void 0;
}
}
next.failureDestination = nextFd;
}
return next;
}
function mergeCronFailureAlert(existing, patch) {
if (patch === false) return false;
if (patch === void 0) return existing;
const next = { ...existing === false || existing === void 0 ? {} : existing };
if ("after" in patch) {
const after = typeof patch.after === "number" && Number.isFinite(patch.after) ? patch.after : 0;
next.after = after > 0 ? Math.floor(after) : void 0;
}
if ("channel" in patch) next.channel = normalizeOptionalTrimmedString(patch.channel);
if ("to" in patch) next.to = normalizeOptionalTrimmedString(patch.to);
if ("cooldownMs" in patch) {
const cooldownMs = typeof patch.cooldownMs === "number" && Number.isFinite(patch.cooldownMs) ? patch.cooldownMs : -1;
next.cooldownMs = cooldownMs >= 0 ? Math.floor(cooldownMs) : void 0;
}
if ("mode" in patch) {
const mode = typeof patch.mode === "string" ? patch.mode.trim() : "";
next.mode = mode === "announce" || mode === "webhook" ? mode : void 0;
}
if ("accountId" in patch) {
const accountId = typeof patch.accountId === "string" ? patch.accountId.trim() : "";
next.accountId = accountId ? accountId : void 0;
}
return next;
}
function isJobDue(job, nowMs, opts) {
if (!job.state) job.state = {};
if (typeof job.state.runningAtMs === "number") return false;
if (opts.forced) return true;
return job.enabled && typeof job.state.nextRunAtMs === "number" && nowMs >= job.state.nextRunAtMs;
}
function resolveJobPayloadTextForMain(job) {
if (job.payload.kind !== "systemEvent") return;
const text = normalizePayloadToSystemText(job.payload);
return text.trim() ? text : void 0;
}
//#endregion
//#region src/cron/service/locked.ts
const storeLocks = /* @__PURE__ */ new Map();
const resolveChain = (promise) => promise.then(() => void 0, () => void 0);
async function locked(state, fn) {
const storePath = state.deps.storePath;
const storeOp = storeLocks.get(storePath) ?? Promise.resolve();
const next = Promise.all([resolveChain(state.op), resolveChain(storeOp)]).then(fn);
const keepAlive = resolveChain(next);
state.op = keepAlive;
storeLocks.set(storePath, keepAlive);
return await next;
}
//#endregion
//#region src/cron/service/store.ts
async function getFileMtimeMs(path) {
try {
return (await fs.promises.stat(path)).mtimeMs;
} catch {
return null;
}
}
async function ensureLoaded(state, opts) {
if (state.store && !opts?.forceReload) return;
const fileMtimeMs = await getFileMtimeMs(state.deps.storePath);
const jobs = (await loadCronStore(state.deps.storePath)).jobs ?? [];
const { mutated } = normalizeStoredCronJobs(jobs);
state.store = {
version: 1,
jobs
};
state.storeLoadedAtMs = state.deps.nowMs();
state.storeFileMtimeMs = fileMtimeMs;
if (!opts?.skipRecompute) recomputeNextRuns(state);
if (mutated) await persist(state, { skipBackup: true });
}
function warnIfDisabled(state, action) {
if (state.deps.cronEnabled) return;
if (state.warnedDisabled) return;
state.warnedDisabled = true;
state.deps.log.warn({
enabled: false,
action,
storePath: state.deps.storePath
}, "cron: scheduler disabled; jobs will not run automatically");
}
async function persist(state, opts) {
if (!state.store) return;
await saveCronStore(state.deps.storePath, state.store, opts);
state.storeFileMtimeMs = await getFileMtimeMs(state.deps.storePath);
}
//#endregion
//#region src/cron/session-reaper.ts
/**
* Cron session reaper — prunes completed isolated cron run sessions
* from the session store after a configurable retention period.
*
* Pattern: sessions keyed as `...:cron:<jobId>:run:<uuid>` are ephemeral
* run records. The base session (`...:cron:<jobId>`) is kept as-is.
*/
const DEFAULT_RETENTION_MS = 24 * 36e5;
/** Minimum interval between reaper sweeps (avoid running every timer tick). */
const MIN_SWEEP_INTERVAL_MS = 5 * 6e4;
const lastSweepAtMsByStore = /* @__PURE__ */ new Map();
function resolveRetentionMs(cronConfig) {
if (cronConfig?.sessionRetention === false) return null;
const raw = cronConfig?.sessionRetention;
if (typeof raw === "string" && raw.trim()) try {
return parseDurationMs(raw.trim(), { defaultUnit: "h" });
} catch {
return DEFAULT_RETENTION_MS;
}
return DEFAULT_RETENTION_MS;
}
/**
* Sweep the session store and prune expired cron run sessions.
* Designed to be called from the cron timer tick — self-throttles via
* MIN_SWEEP_INTERVAL_MS to avoid excessive I/O.
*
* Lock ordering: this function acquires the session-store file lock via
* `updateSessionStore`. It must be called OUTSIDE of the cron service's
* own `locked()` section to avoid lock-order inversions. The cron timer
* calls this after all `locked()` sections have been released.
*/
async function sweepCronRunSessions(params) {
const now = params.nowMs ?? Date.now();
const storePath = params.sessionStorePath;
const lastSweepAtMs = lastSweepAtMsByStore.get(storePath) ?? 0;
if (!params.force && now - lastSweepAtMs < MIN_SWEEP_INTERVAL_MS) return {
swept: false,
pruned: 0
};
const retentionMs = resolveRetentionMs(params.cronConfig);
if (retentionMs === null) {
lastSweepAtMsByStore.set(storePath, now);
return {
swept: false,
pruned: 0
};
}
let pruned = 0;
const prunedSessions = /* @__PURE__ */ new Map();
try {
await updateSessionStore(storePath, (store) => {
const cutoff = now - retentionMs;
for (const key of Object.keys(store)) {
if (!isCronRunSessionKey(key)) continue;
const entry = store[key];
if (!entry) continue;
if ((entry.updatedAt ?? 0) < cutoff) {
if (!prunedSessions.has(entry.sessionId) || entry.sessionFile) prunedSessions.set(entry.sessionId, entry.sessionFile);
delete store[key];
pruned++;
}
}
});
} catch (err) {
params.log.warn({ err: String(err) }, "cron-reaper: failed to sweep session store");
return {
swept: false,
pruned: 0
};
}
lastSweepAtMsByStore.set(storePath, now);
if (prunedSessions.size > 0) try {
const store = loadSessionStore(storePath, { skipCache: true });
const archivedDirs = archiveRemovedSessionTranscripts({
removedSessionFiles: prunedSessions,
referencedSessionIds: new Set(Object.values(store).map((entry) => entry?.sessionId).filter((id) => Boolean(id))),
storePath,
reason: "deleted",
restrictToStoreDir: true
});
if (archivedDirs.size > 0) await cleanupArchivedSessionTranscripts({
directories: [...archivedDirs],
olderThanMs: retentionMs,
reason: "deleted",
nowMs: now
});
} catch (err) {
params.log.warn({ err: String(err) }, "cron-reaper: transcript cleanup failed");
}
if (pruned > 0) params.log.info({
pruned,
retentionMs
}, `cron-reaper: pruned ${pruned} expired cron run session(s)`);
return {
swept: true,
pruned
};
}
//#endregion
//#region src/cron/service/timeout-policy.ts
/**
* Maximum wall-clock time for a single job execution. Acts as a safety net
* on top of per-provider/per-agent timeouts to prevent one stuck job from
* wedging the entire cron lane.
*/
const DEFAULT_JOB_TIMEOUT_MS = 10 * 6e4;
/**
* Agent turns can legitimately run much longer than generic cron jobs.
* Use a larger safety ceiling when no explicit timeout is set.
*/
const AGENT_TURN_SAFETY_TIMEOUT_MS = 60 * 6e4;
function resolveCronJobTimeoutMs(job) {
const configuredTimeoutMs = job.payload.kind === "agentTurn" && typeof job.payload.timeoutSeconds === "number" ? Math.floor(job.payload.timeoutSeconds * 1e3) : void 0;
if (configuredTimeoutMs === void 0) return job.payload.kind === "agentTurn" ? AGENT_TURN_SAFETY_TIMEOUT_MS : DEFAULT_JOB_TIMEOUT_MS;
return configuredTimeoutMs <= 0 ? void 0 : configuredTimeoutMs;
}
//#endregion
//#region src/cron/service/timer.ts
const MAX_TIMER_DELAY_MS = 6e4;
/**
* Minimum gap between consecutive fires of the same cron job. This is a
* safety net that prevents spin-loops when `computeJobNextRunAtMs` returns
* a value within the same second as the just-completed run. The guard
* is intentionally generous (2 s) so it never masks a legitimate schedule
* but always breaks an infinite re-trigger cycle. (See #17821)
*/
const MIN_REFIRE_GAP_MS = 2e3;
const DEFAULT_MISSED_JOB_STAGGER_MS = 5e3;
const DEFAULT_MAX_MISSED_JOBS_PER_RESTART = 5;
const DEFAULT_FAILURE_ALERT_AFTER = 2;
const DEFAULT_FAILURE_ALERT_COOLDOWN_MS = 60 * 6e4;
async function executeJobCoreWithTimeout(state, job) {
const jobTimeoutMs = resolveCronJobTimeoutMs(job);
if (typeof jobTimeoutMs !== "number") return await executeJobCore(state, job);
const runAbortController = new AbortController();
let timeoutId;
try {
return await Promise.race([executeJobCore(state, job, runAbortController.signal), new Promise((_, reject) => {
timeoutId = setTimeout(() => {
runAbortController.abort(timeoutErrorMessage());
reject(new Error(timeoutErrorMessage()));
}, jobTimeoutMs);
})]);
} finally {
if (timeoutId) clearTimeout(timeoutId);
}
}
function resolveRunConcurrency(state) {
const raw = state.deps.cronConfig?.maxConcurrentRuns;
if (typeof raw !== "number" || !Number.isFinite(raw)) return 1;
return Math.max(1, Math.floor(raw));
}
function timeoutErrorMessage() {
return "cron: job execution timed out";
}
function isAbortError(err) {
if (!(err instanceof Error)) return false;
return err.name === "AbortError" || err.message === timeoutErrorMessage();
}
/**
* Exponential backoff delays (in ms) indexed by consecutive error count.
* After the last entry the delay stays constant.
*/
const DEFAULT_BACKOFF_SCHEDULE_MS = [
3e4,
6e4,
5 * 6e4,
15 * 6e4,
60 * 6e4
];
function errorBackoffMs(consecutiveErrors, scheduleMs = DEFAULT_BACKOFF_SCHEDULE_MS) {
const idx = Math.min(consecutiveErrors - 1, scheduleMs.length - 1);
return scheduleMs[Math.max(0, idx)];
}
/** Default max retries for one-shot jobs on transient errors (#24355). */
const DEFAULT_MAX_TRANSIENT_RETRIES = 3;
const TRANSIENT_PATTERNS = {
rate_limit: /(rate[_ ]limit|too many requests|429|resource has been exhausted|cloudflare|tokens per day)/i,
overloaded: /\b529\b|\boverloaded(?:_error)?\b|high demand|temporar(?:ily|y) overloaded|capacity exceeded/i,
network: /(network|econnreset|econnrefused|fetch failed|socket)/i,
timeout: /(timeout|etimedout)/i,
server_error: /\b5\d{2}\b/
};
function isTransientCronError(error, retryOn) {
if (!error || typeof error !== "string") return false;
return (retryOn?.length ? retryOn : Object.keys(TRANSIENT_PATTERNS)).some((k) => TRANSIENT_PATTERNS[k]?.test(error));
}
function resolveRetryConfig(cronConfig) {
const retry = cronConfig?.retry;
return {
maxAttempts: typeof retry?.maxAttempts === "number" ? retry.maxAttempts : DEFAULT_MAX_TRANSIENT_RETRIES,
backoffMs: Array.isArray(retry?.backoffMs) && retry.backoffMs.length > 0 ? retry.backoffMs : DEFAULT_BACKOFF_SCHEDULE_MS.slice(0, 3),
retryOn: Array.isArray(retry?.retryOn) && retry.retryOn.length > 0 ? retry.retryOn : void 0
};
}
function resolveDeliveryStatus(params) {
if (params.delivered === true) return "delivered";
if (params.delivered === false) return "not-delivered";
return resolveCronDeliveryPlan(params.job).requested ? "unknown" : "not-requested";
}
function normalizeCronMessageChannel(input) {
if (typeof input !== "string") return;
const channel = input.trim().toLowerCase();
return channel ? channel : void 0;
}
function normalizeTo(input) {
if (typeof input !== "string") return;
const to = input.trim();
return to ? to : void 0;
}
function clampPositiveInt(value, fallback) {
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
const floored = Math.floor(value);
return floored >= 1 ? floored : fallback;
}
function clampNonNegativeInt(value, fallback) {
if (typeof value !== "number" || !Number.isFinite(value)) return fallback;
const floored = Math.floor(value);
return floored >= 0 ? floored : fallback;
}
function resolveFailureAlert(state, job) {
const globalConfig = state.deps.cronConfig?.failureAlert;
const jobConfig = job.failureAlert === false ? void 0 : job.failureAlert;
if (job.failureAlert === false) return null;
if (!jobConfig && globalConfig?.enabled !== true) return null;
const mode = jobConfig?.mode ?? globalConfig?.mode;
const explicitTo = normalizeTo(jobConfig?.to);
return {
after: clampPositiveInt(jobConfig?.after ?? globalConfig?.after, DEFAULT_FAILURE_ALERT_AFTER),
cooldownMs: clampNonNegativeInt(jobConfig?.cooldownMs ?? globalConfig?.cooldownMs, DEFAULT_FAILURE_ALERT_COOLDOWN_MS),
channel: normalizeCronMessageChannel(jobConfig?.channel) ?? normalizeCronMessageChannel(job.delivery?.channel) ?? "last",
to: mode === "webhook" ? explicitTo : explicitTo ?? normalizeTo(job.delivery?.to),
mode,
accountId: jobConfig?.accountId ?? globalConfig?.accountId
};
}
function emitFailureAlert(state, params) {
const safeJobName = params.job.name || params.job.id;
const truncatedError = (params.error?.trim() || "unknown error").slice(0, 200);
const text = [`Cron job "${safeJobName}" failed ${params.consecutiveErrors} times`, `Last error: ${truncatedError}`].join("\n");
if (state.deps.sendCronFailureAlert) {
state.deps.sendCronFailureAlert({
job: params.job,
text,
channel: params.channel,
to: params.to,
mode: params.mode,
accountId: params.accountId
}).catch((err) => {
state.deps.log.warn({
jobId: params.job.id,
err: String(err)
}, "cron: failure alert delivery failed");
});
return;
}
state.deps.enqueueSystemEvent(text, { agentId: params.job.agentId });
if (params.job.wakeMode === "now") state.deps.requestHeartbeatNow({ reason: `cron:${params.job.id}:failure-alert` });
}
/**
* Apply the result of a job execution to the job's state.
* Handles consecutive error tracking, exponential backoff, one-shot disable,
* and nextRunAtMs computation. Returns `true` if the job should be deleted.
*/
function applyJobResult(state, job, result, opts) {
const prevLastRunAtMs = job.state.lastRunAtMs;
const computeNextWithPreservedLastRun = (nowMs) => {
const saved = job.state.lastRunAtMs;
job.state.lastRunAtMs = prevLastRunAtMs;
try {
return computeJobNextRunAtMs(job, nowMs);
} finally {
job.state.lastRunAtMs = saved;
}
};
job.state.runningAtMs = void 0;
job.state.lastRunAtMs = result.startedAt;
job.state.lastRunStatus = result.status;
job.state.lastStatus = result.status;
job.state.lastDurationMs = Math.max(0, result.endedAt - result.startedAt);
job.state.lastError = result.error;
job.state.lastErrorReason = result.status === "error" && typeof result.error === "string" ? resolveFailoverReasonFromError(result.error) ?? void 0 : void 0;
job.state.lastDelivered = result.delivered;
const deliveryStatus = resolveDeliveryStatus({
job,
delivered: result.delivered
});
job.state.lastDeliveryStatus = deliveryStatus;
job.state.lastDeliveryError = deliveryStatus === "not-delivered" && result.error ? result.error : void 0;
job.updatedAtMs = result.endedAt;
if (result.status === "error") {
job.state.consecutiveErrors = (job.state.consecutiveErrors ?? 0) + 1;
const alertConfig = resolveFailureAlert(state, job);
if (alertConfig && job.state.consecutiveErrors >= alertConfig.after) {
if (!(job.delivery?.bestEffort === true || job.payload.kind === "agentTurn" && job.payload.bestEffortDeliver === true)) {
const now = state.deps.nowMs();
const lastAlert = job.state.lastFailureAlertAtMs;
if (!(typeof lastAlert === "number" && now - lastAlert < Math.max(0, alertConfig.cooldownMs))) {
emitFailureAlert(state, {
job,
error: result.error,
consecutiveErrors: job.state.consecutiveErrors,
channel: alertConfig.channel,
to: alertConfig.to,
mode: alertConfig.mode,
accountId: alertConfig.accountId
});
job.state.lastFailureAlertAtMs = now;
}
}
}
} else {
job.state.consecutiveErrors = 0;
job.state.lastFailureAlertAtMs = void 0;
}
const shouldDelete = job.schedule.kind === "at" && job.deleteAfterRun === true && result.status === "ok";
if (!shouldDelete) if (job.schedule.kind === "at") {
if (result.status === "ok" || result.status === "skipped") {
job.enabled = false;
job.state.nextRunAtMs = void 0;
} else if (result.status === "error") {
const retryConfig = resolveRetryConfig(state.deps.cronConfig);
const transient = isTransientCronError(result.error, retryConfig.retryOn);
const consecutive = job.state.consecutiveErrors;
if (transient && consecutive <= retryConfig.maxAttempts) {
const backoff = errorBackoffMs(consecutive, retryConfig.backoffMs);
job.state.nextRunAtMs = result.endedAt + backoff;
state.deps.log.info({
jobId: job.id,
jobName: job.name,
consecutiveErrors: consecutive,
backoffMs: backoff,
nextRunAtMs: job.state.nextRunAtMs
}, "cron: scheduling one-shot retry after transient error");
} else {
job.enabled = false;
job.state.nextRunAtMs = void 0;
state.deps.log.warn({
jobId: job.id,
jobName: job.name,
consecutiveErrors: consecutive,
error: result.error,
reason: transient ? "max retries exhausted" : "permanent error"
}, "cron: disabling one-shot job after error");
}
}
} else if (result.status === "error" && job.enabled) {
const backoff = errorBackoffMs(job.state.consecutiveErrors ?? 1);
let normalNext;
try {
normalNext = opts?.preserveSchedule && job.schedule.kind === "every" ? computeNextWithPreservedLastRun(result.endedAt) : computeJobNextRunAtMs(job, result.endedAt);
} catch (err) {
recordScheduleComputeError({
state,
job,
err
});
}
const backoffNext = result.endedAt + backoff;
job.state.nextRunAtMs = normalNext !== void 0 ? Math.max(normalNext, backoffNext) : backoffNext;
state.deps.log.info({
jobId: job.id,
consecutiveErrors: job.state.consecutiveErrors,
backoffMs: backoff,
nextRunAtMs: job.state.nextRunAtMs
}, "cron: applying error backoff");
} else if (job.enabled) {
let naturalNext;
try {
naturalNext = opts?.preserveSchedule && job.schedule.kind === "every" ? computeNextWithPreservedLastRun(result.endedAt) : computeJobNextRunAtMs(job, result.endedAt);
} catch (err) {
recordScheduleComputeError({
state,
job,
err
});
}
if (job.schedule.kind === "cron") {
const minNext = result.endedAt + MIN_REFIRE_GAP_MS;
job.state.nextRunAtMs = naturalNext !== void 0 ? Math.max(naturalNext, minNext) : minNext;
} else job.state.nextRunAtMs = naturalNext;
} else job.state.nextRunAtMs = void 0;
return shouldDelete;
}
function applyOutcomeToStoredJob(state, result) {
const store = state.store;
if (!store) return;
const jobs = store.jobs;
const job = jobs.find((entry) => entry.id === result.jobId);
if (!job) {
state.deps.log.warn({ jobId: result.jobId }, "cron: applyOutcomeToStoredJob — job not found after forceReload, result discarded");
return;
}
const shouldDelete = applyJobResult(state, job, {
status: result.status,
error: result.error,
delivered: result.delivered,
startedAt: result.startedAt,
endedAt: result.endedAt
});
emitJobFinished(state, job, result, result.startedAt);
if (shouldDelete) {
store.jobs = jobs.filter((entry) => entry.id !== job.id);
emit(state, {
jobId: job.id,
action: "removed"
});
}
}
function armTimer(state) {
if (state.timer) clearTimeout(state.timer);
state.timer = null;
if (!state.deps.cronEnabled) {
state.deps.log.debug({}, "cron: armTimer skipped - scheduler disabled");
return;
}
const nextAt = nextWakeAtMs(state);
if (!nextAt) {
const jobCount = state.store?.jobs.length ?? 0;
const enabledCount = state.store?.jobs.filter((j) => j.enabled).length ?? 0;
const withNextRun = state.store?.jobs.filter((j) => j.enabled && typeof j.state.nextRunAtMs === "number" && Number.isFinite(j.state.nextRunAtMs)).length ?? 0;
state.deps.log.debug({
jobCount,
enabledCount,
withNextRun
}, "cron: armTimer skipped - no jobs with nextRunAtMs");
return;
}
const now = state.deps.nowMs();
const delay = Math.max(nextAt - now, 0);
const flooredDelay = delay === 0 ? MIN_REFIRE_GAP_MS : delay;
const clampedDelay = Math.min(flooredDelay, MAX_TIMER_DELAY_MS);
state.timer = setTimeout(() => {
onTimer(state).catch((err) => {
state.deps.log.error({ err: String(err) }, "cron: timer tick failed");
});
}, clampedDelay);
state.deps.log.debug({
nextAt,
delayMs: clampedDelay,
clamped: delay > MAX_TIMER_DELAY_MS
}, "cron: timer armed");
}
function armRunningRecheckTimer(state) {
if (state.timer) clearTimeout(state.timer);
state.timer = setTimeout(() => {
onTimer(state).catch((err) => {
state.deps.log.error({ err: String(err) }, "cron: timer tick failed");
});
}, MAX_TIMER_DELAY_MS);
}
async function onTimer(state) {
if (state.running) {
armRunningRecheckTimer(state);
return;
}
state.running = true;
armRunningRecheckTimer(state);
try {
const dueJobs = await locked(state, async () => {
await ensureLoaded(state, {
forceReload: true,
skipRecompute: true
});
const dueCheckNow = state.deps.nowMs();
const due = collectRunnableJobs(state, dueCheckNow);
if (due.length === 0) {
if (recomputeNextRunsForMaintenance(state, {
recomputeExpired: true,
nowMs: dueCheckNow
})) await persist(state);
return [];
}
const now = state.deps.nowMs();
for (const job of due) {
job.state.runningAtMs = now;
job.state.lastError = void 0;
}
await persist(state);
return due.map((j) => ({
id: j.id,
job: j
}));
});
const runDueJob = async (params) => {
const { id, job } = params;
const startedAt = state.deps.nowMs();
job.state.runningAtMs = startedAt;
emit(state, {
jobId: job.id,
action: "started",
runAtMs: startedAt
});
const jobTimeoutMs = resolveCronJobTimeoutMs(job);
try {
return {
jobId: id,
...await executeJobCoreWithTimeout(state, job),
startedAt,
endedAt: state.deps.nowMs()
};
} catch (err) {
const errorText = isAbortError(err) ? timeoutErrorMessage() : String(err);
state.deps.log.warn({
jobId: id,
jobName: job.name,
timeoutMs: jobTimeoutMs ?? null
}, `cron: job failed: ${errorText}`);
return {
jobId: id,
status: "error",
error: errorText,
startedAt,
endedAt: state.deps.nowMs()
};
}
};
const concurrency = Math.min(resolveRunConcurrency(state), Math.max(1, dueJobs.length));
const results = Array.from({ length: dueJobs.length });
let cursor = 0;
const workers = Array.from({ length: concurrency }, async () => {
for (;;) {
const index = cursor++;
if (index >= dueJobs.length) return;
const due = dueJobs[index];
if (!due) return;
results[index] = await runDueJob(due);
}
});
await Promise.all(workers);
const completedResults = results.filter((entry) => entry !== void 0);
if (completedResults.length > 0) await locked(state, async () => {
await ensureLoaded(state, {
forceReload: true,
skipRecompute: true
});
for (const result of completedResults) applyOutcomeToStoredJob(state, result);
recomputeNextRunsForMaintenance(state);
await persist(state);
});
} finally {
const storePaths = /* @__PURE__ */ new Set();
if (state.deps.resolveSessionStorePath) {
const defaultAgentId = state.deps.defaultAgentId ?? "main";
if (state.store?.jobs?.length) for (const job of state.store.jobs) {
const agentId = typeof job.agentId === "string" && job.agentId.trim() ? job.agentId : defaultAgentId;
storePaths.add(state.deps.resolveSessionStorePath(agentId));
}
else storePaths.add(state.deps.resolveSessionStorePath(defaultAgentId));
} else if (state.deps.sessionStorePath) storePaths.add(state.deps.sessionStorePath);
if (storePaths.size > 0) {
const nowMs = state.deps.nowMs();
for (const storePath of storePaths) try {
await sweepCronRunSessions({
cronConfig: state.deps.cronConfig,
sessionStorePath: storePath,
nowMs,
log: state.deps.log
});
} catch (err) {
state.deps.log.warn({
err: String(err),
storePath
}, "cron: session reaper sweep failed");
}
}
state.running = false;
armTimer(state);
}
}
function isRunnableJob(params) {
const { job, nowMs } = params;
if (!job.state) job.state = {};
if (!job.enabled) return false;
if (params.skipJobIds?.has(job.id)) return false;
if (typeof job.state.runningAtMs === "number") return false;
if (params.skipAtIfAlreadyRan && job.schedule.kind === "at" && job.state.lastStatus) {
const lastRun = job.state.lastRunAtMs;
const nextRun = job.state.nextRunAtMs;
if (job.state.lastStatus === "error" && job.enabled && typeof nextRun === "number" && typeof lastRun === "number" && nextRun > lastRun) return nowMs >= nextRun;
return false;
}
const next = job.state.nextRunAtMs;
if (typeof next === "number" && Number.isFinite(next) && nowMs >= next) return true;
if (typeof next === "number" && Number.isFinite(next) && next > nowMs && isErrorBackoffPending(job, nowMs)) return false;
if (!params.allowCronMissedRunByLastRun || job.schedule.kind !== "cron") return false;
let previousRunAtMs;
try {
previousRunAtMs = computeJobPreviousRunAtMs(job, nowMs);
} catch {
return false;
}
if (typeof previousRunAtMs !== "number" || !Number.isFinite(previousRunAtMs)) return false;
const lastRunAtMs = job.state.lastRunAtMs;
if (typeof lastRunAtMs !== "number" || !Number.isFinite(lastRunAtMs)) return false;
return previousRunAtMs > lastRunAtMs;
}
function isErrorBackoffPending(job, nowMs) {
if (job.schedule.kind === "at" || job.state.lastStatus !== "error") return false;
const lastRunAtMs = job.state.lastRunAtMs;
if (typeof lastRunAtMs !== "number" || !Number.isFinite(lastRunAtMs)) return false;
const consecutiveErrorsRaw = job.state.consecutiveErrors;
return nowMs < lastRunAtMs + errorBackoffMs(typeof consecutiveErrorsRaw === "number" && Number.isFinite(consecutiveErrorsRaw) ? Math.max(1, Math.floor(consecutiveErrorsRaw)) : 1);
}
function collectRunnableJobs(state, nowMs, opts) {
if (!state.store) return [];
return state.store.jobs.filter((job) => isRunnableJob({
job,
nowMs,
skipJobIds: opts?.skipJobIds,
skipAtIfAlreadyRan: opts?.skipAtIfAlreadyRan,
allowCronMissedRunByLastRun: opts?.allowCronMissedRunByLastRun
}));
}
async function runMissedJobs(state, opts) {
const plan = await planStartupCatchup(state, opts);
if (plan.candidates.length === 0 && plan.deferredJobIds.length === 0) return;
await applyStartupCatchupOutcomes(state, plan, await executeStartupCatchupPlan(state, plan));
}
async function planStartupCatchup(state, opts) {
const maxImmediate = Math.max(0, state.deps.maxMissedJobsPerRestart ?? DEFAULT_MAX_MISSED_JOBS_PER_RESTART);
return locked(state, async () => {
await ensureLoaded(state, { skipRecompute: true });
if (!state.store) return {
candidates: [],
deferredJobIds: []
};
const now = state.deps.nowMs();
const missed = collectRunnableJobs(state, now, {
skipJobIds: opts?.skipJobIds,
skipAtIfAlreadyRan: true,
allowCronMissedRunByLastRun: true
});
if (missed.length === 0) return {
candidates: [],
deferredJobIds: []
};
const sorted = missed.toSorted((a, b) => (a.state.nextRunAtMs ?? 0) - (b.state.nextRunAtMs ?? 0));
const startupCandidates = sorted.slice(0, maxImmediate);
const deferred = sorted.slice(maxImmediate);
if (deferred.length > 0) state.deps.log.info({
immediateCount: startupCandidates.length,
deferredCount: deferred.length,
totalMissed: missed.length
}, "cron: staggering missed jobs to prevent gateway overload");
if (startupCandidates.length > 0) state.deps.log.info({
count: startupCandidates.length,
jobIds: startupCandidates.map((j) => j.id)
}, "cron: running missed jobs after restart");
for (const job of startupCandidates) {
job.state.runningAtMs = now;
job.state.lastError = void 0;
}
await persist(state);
return {
candidates: startupCandidates.map((job) => ({
jobId: job.id,
job
})),
deferredJobIds: deferred.map((job) => job.id)
};
});
}
async function executeStartupCatchupPlan(state, plan) {
const outcomes = [];
for (const candidate of plan.candidates) outcomes.push(await runStartupCatchupCandidate(state, candidate));
return outcomes;
}
async function runStartupCatchupCandidate(state, candidate) {
const startedAt = state.deps.nowMs();
emit(state, {
jobId: candidate.job.id,
action: "started",
runAtMs: startedAt
});
try {
const result = await executeJobCoreWithTimeout(state, candidate.job);
return {
jobId: candidate.jobId,
status: result.status,
error: result.error,
summary: result.summary,
delivered: result.delivered,
sessionId: result.sessionId,
sessionKey: result.sessionKey,
model: result.model,
provider: result.provider,
usage: result.usage,
startedAt,
endedAt: state.deps.nowMs()
};
} catch (err) {
return {
jobId: candidate.jobId,
status: "error",
error: String(err),
startedAt,
endedAt: state.deps.nowMs()
};
}
}
async function applyStartupCatchupOutcomes(state, plan, outcomes) {
const staggerMs = Math.max(0, state.deps.missedJobStaggerMs ?? DEFAULT_MISSED_JOB_STAGGER_MS);
await locked(state, async () => {
await ensureLoaded(state, { skipRecompute: true });
if (!state.store) return;
for (const result of outcomes) applyOutcomeToStoredJob(state, result);
if (plan.deferredJobIds.length > 0) {
const baseNow = state.deps.nowMs();
let offset = staggerMs;
for (const jobId of plan.deferredJobIds) {
const job = state.store.jobs.find((entry) => entry.id === jobId);
if (!job || !job.enabled) continue;
job.state.nextRunAtMs = baseNow + offset;
offset += staggerMs;
}
}
recomputeNextRunsForMaintenance(state);
await persist(state);
});
}
async function executeJobCore(state, job, abortSignal) {
const resolveAbortError = () => ({
status: "error",
error: timeoutErrorMessage()
});
const waitWithAbort = async (ms) => {
if (!abortSignal) {
await new Promise((resolve) => setTimeout(resolve, ms));
return;
}
if (abortSignal.aborted) return;
await new Promise((resolve) => {
const timer = setTimeout(() => {
abortSignal.removeEventListener("abort", onAbort);
resolve();
}, ms);
const onAbort = () => {
clearTimeout(timer);
abortSignal.removeEventListener("abort", onAbort);
resolve();
};
abortSignal.addEventListener("abort", onAbort, { once: true });
});
};
if (abortSignal?.aborted) return resolveAbortError();
if (job.sessionTarget === "main") {
const text = resolveJobPayloadTextForMain(job);
if (!text) return {
status: "skipped",
error: job.payload.kind === "systemEvent" ? "main job requires non-empty systemEvent text" : "main job requires payload.kind=\"systemEvent\""
};
const targetMainSessionKey = job.sessionKey;
state.deps.enqueueSystemEvent(text, {
agentId: job.agentId,
sessionKey: targetMainSessionKey,
contextKey: `cron:${job.id}`
});
if (job.wakeMode === "now" && state.deps.runHeartbeatOnce) {
const reason = `cron:${job.id}`;
const maxWaitMs = state.deps.wakeNowHeartbeatBusyMaxWaitMs ?? 2 * 6e4;
const retryDelayMs = state.deps.wakeNowHeartbeatBusyRetryDelayMs ?? 250;
const waitStartedAt = state.deps.nowMs();
let heartbeatResult;
for (;;) {
if (abortSignal?.aborted) return resolveAbortError();
heartbeatResult = await state.deps.runHeartbeatOnce({
reason,
agentId: job.agentId,
sessionKey: targetMainSessionKey,
heartbeat: { target: "last" }
});
if (heartbeatResult.status !== "skipped" || heartbeatResult.reason !== "requests-in-flight") break;
if (abortSignal?.aborted) return resolveAbortError();
if (state.deps.nowMs() - waitStartedAt > maxWaitMs) {
if (abortSignal?.aborted) return resolveAbortError();
state.deps.requestHeartbeatNow({
reason,
agentId: job.agentId,
sessionKey: targetMainSessionKey
});
return {
status: "ok",
summary: text
};
}
await waitWithAbort(retryDelayMs);
}
if (heartbeatResult.status === "ran") return {
status: "ok",
summary: text
};
else if (heartbeatResult.status === "skipped") return {
status: "skipped",
error: heartbeatResult.reason,
summary: text
};
else return {
status: "error",
error: heartbeatResult.reason,
summary: text
};
} else {
if (abortSignal?.aborted) return resolveAbortError();
state.deps.requestHeartbeatNow({
reason: `cron:${job.id}`,
agentId: job.agentId,
sessionKey: targetMainSessionKey
});
return {
status: "ok",
summary: text
};
}
}
if (job.payload.kind !== "agentTurn") return {
status: "skipped",
error: "isolated job requires payload.kind=agentTurn"
};
if (abortSignal?.aborted) return resolveAbortError();
const res = await state.deps.runIsolatedAgentJob({
job,
message: job.payload.message,
abortSignal
});
if (abortSignal?.aborted) return {
status: "error",
error: timeoutErrorMessage()
};
return {
status: res.status,
error: res.error,
summary: res.summary,
delivered: res.delivered,
deliveryAttempted: res.deliveryAttempted,
sessionId: res.sessionId,
sessionKey: res.sessionKey,
model: res.model,
provider: res.provider,
usage: res.usage
};
}
function emitJobFinished(state, job, result, runAtMs) {
emit(state, {
jobId: job.id,
action: "finished",
status: result.status,
error: result.error,
summary: result.summary,
delivered: result.delivered,
deliveryStatus: job.state.lastDeliveryStatus,
deliveryError: job.state.lastDeliveryError,
sessionId: result.sessionId,
sessionKey: result.sessionKey,
runAtMs,
durationMs: job.state.lastDurationMs,
nextRunAtMs: job.state.nextRunAtMs,
model: result.model,
provider: result.provider,
usage: result.usage
});
}
function wake(state, opts) {
const text = opts.text.trim();
if (!text) return { ok: false };
state.deps.enqueueSystemEvent(text);
if (opts.mode === "now") state.deps.requestHeartbeatNow({ reason: "wake" });
return { ok: true };
}
function stopTimer(state) {
if (state.timer) clearTimeout(state.timer);
state.timer = null;
}
function emit(state, evt) {
try {
state.deps.onEvent?.(evt);
} catch {}
}
//#endregion
//#region src/cron/service/ops.ts
function mergeManualRunSnapshotAfterReload(params) {
if (!params.state.store) return;
if (params.removed) {
params.state.store.jobs = params.state.store.jobs.filter((job) => job.id !== params.jobId);
return;
}
if (!params.snapshot) return;
const reloaded = params.state.store.jobs.find((job) => job.id === params.jobId);
if (!reloaded) return;
reloaded.enabled = params.snapshot.enabled;
reloaded.updatedAtMs = params.snapshot.updatedAtMs;
reloaded.state = params.snapshot.state;
}
async function ensureLoadedForRead(state) {
await ensureLoaded(state, { skipRecompute: true });
if (!state.store) return;
if (recomputeNextRunsForMaintenance(state)) await persist(state);
}
async function start(state) {
if (!state.deps.cronEnabled) {
state.deps.log.info({ enabled: false }, "cron: disabled");
return;
}
const startupInterruptedJobIds = /* @__PURE__ */ new Set();
await locked(state, async () => {
await ensureLoaded(state, { skipRecompute: true });
const jobs = state.store?.jobs ?? [];
for (const job of jobs) if (typeof job.state.runningAtMs === "number") {
state.deps.log.warn({
jobId: job.id,
runningAtMs: job.state.runningAtMs
}, "cron: clearing stale running marker on startup");
job.state.runningAtMs = void 0;
startupInterruptedJobIds.add(job.id);
}
if (startupInterruptedJobIds.size > 0) await persist(state);
});
await runMissedJobs(state, { skipJobIds: startupInterruptedJobIds });
await locked(state, async () => {
await ensureLoaded(state, { skipRecompute: true });
if (recomputeNextRuns(state)) await persist(state);
armTimer(state);
state.deps.log.info({
enabled: true,
jobs: state.store?.jobs.length ?? 0,
nextWakeAtMs: nextWakeAtMs(state) ?? null
}, "cron: started");
});
}
function stop(state) {
stopTimer(state);
}
async function status(state) {
return await locked(state, async () => {
await ensureLoadedForRead(state);
return {
enabled: state.deps.cronEnabled,
storePath: state.deps.storePath,
jobs: state.store?.jobs.length ?? 0,
nextWakeAtMs: state.deps.cronEnabled ? nextWakeAtMs(state) ?? null : null
};
});
}
async function list(state, opts) {
return await locked(state, async () => {
await ensureLoadedForRead(state);
const includeDisabled = opts?.includeDisabled === true;
return (state.store?.jobs ?? []).filter((j) => includeDisabled || j.enabled).toSorted((a, b) => (a.state.nextRunAtMs ?? 0) - (b.state.nextRunAtMs ?? 0));
});
}
function resolveEnabledFilter(opts) {
if (opts?.enabled === "all" || opts?.enabled === "enabled" || opts?.enabled === "disabled") return opts.enabled;
return opts?.includeDisabled ? "all" : "enabled";
}
function sortJobs(jobs, sortBy, sortDir) {
const dir = sortDir === "desc" ? -1 : 1;
return jobs.toSorted((a, b) => {
let cmp = 0;
if (sortBy === "name") {
const aName = typeof a.name === "string" ? a.name : "";
const bName = typeof b.name === "string" ? b.name : "";
cmp = aName.localeCompare(bName, void 0, { sensitivity: "base" });
} else if (sortBy === "updatedAtMs") cmp = a.updatedAtMs - b.updatedAtMs;
else {
const aNext = a.state.nextRunAtMs;
const bNext = b.state.nextRunAtMs;
if (typeof aNext === "number" && typeof bNext === "number") cmp = aNext - bNext;
else if (typeof aNext === "number") cmp = -1;
else if (typeof bNext === "number") cmp = 1;
else cmp = 0;
}
if (cmp !== 0) return cmp * dir;
const aId = typeof a.id === "string" ? a.id : "";
const bId = typeof b.id === "string" ? b.id : "";
return aId.localeCompare(bId);
});
}
async function listPage(state, opts) {
return await locked(state, async () => {
await ensureLoadedForRead(state);
const query = opts?.query?.trim().toLowerCase() ?? "";
const enabledFilter = resolveEnabledFilter(opts);
const sortBy = opts?.sortBy ?? "nextRunAtMs";
const sortDir = opts?.sortDir ?? "asc";
const sorted = sortJobs((state.store?.jobs ?? []).filter((job) => {
if (enabledFilter === "enabled" && !job.enabled) return false;
if (enabledFilter === "disabled" && job.enabled) return false;
if (!query) return true;
return [
job.name,
job.description ?? "",
job.agentId ?? ""
].join(" ").toLowerCase().includes(query);
}), sortBy, sortDir);
const total = sorted.length;
const offset = Math.max(0, Math.min(total, Math.floor(opts?.offset ?? 0)));
const defaultLimit = total === 0 ? 50 : total;
const limit = Math.max(1, Math.min(200, Math.floor(opts?.limit ?? defaultLimit)));
const jobs = sorted.slice(offset, offset + limit);
const nextOffset = offset + jobs.length;
return {
jobs,
total,
offset,
limit,
hasMore: nextOffset < total,
nextOffset: nextOffset < total ? nextOffset : null
};
});
}
async function add(state, input) {
return await locked(state, async () => {
warnIfDisabled(state, "add");
await ensureLoaded(state);
const job = createJob(state, normalizeCronCreateDeliveryInput(input));
state.store?.jobs.push(job);
recomputeNextRuns(state);
await persist(state);
armTimer(state);
state.deps.log.info({
jobId: job.id,
jobName: job.name,
nextRunAtMs: job.state.nextRunAtMs,
schedulerNextWakeAtMs: nextWakeAtMs(state) ?? null,
timerArmed: state.timer !== null,
cronEnabled: state.deps.cronEnabled
}, "cron: job added");
emit(state, {
jobId: job.id,
action: "added",
nextRunAtMs: job.state.nextRunAtMs
});
return job;
});
}
async function update(state, id, patch) {
return await locked(state, async () => {
warnIfDisabled(state, "update");
await ensureLoaded(state, { skipRecompute: true });
const job = findJobOrThrow(state, id);
const now = state.deps.nowMs();
applyJobPatch(job, patch, { defaultAgentId: state.deps.defaultAgentId });
if (job.schedule.kind === "every") {
const anchor = job.schedule.anchorMs;
if (typeof anchor !== "number" || !Number.isFinite(anchor)) {
const fallbackAnchorMs = patch.schedule?.kind === "every" ? now : typeof job.createdAtMs === "number" && Number.isFinite(job.createdAtMs) ? job.createdAtMs : now;
job.schedule = {
...job.schedule,
anchorMs: Math.max(0, Math.floor(fallbackAnchorMs))
};
}
}
const scheduleChanged = patch.schedule !== void 0;
const enabledChanged = patch.enabled !== void 0;
job.updatedAtMs = now;
if (scheduleChanged || enabledChanged) if (job.enabled) job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
else {
job.state.nextRunAtMs = void 0;
job.state.runningAtMs = void 0;
}
else if (job.enabled) {
const nextRun = job.state.nextRunAtMs;
if (typeof nextRun !== "number" || !Number.isFinite(nextRun)) job.state.nextRunAtMs = computeJobNextRunAtMs(job, now);
}
await persist(state);
armTimer(state);
emit(state, {
jobId: id,
action: "updated",
nextRunAtMs: job.state.nextRunAtMs
});
return job;
});
}
async function remove(state, id) {
return await locked(state, async () => {
warnIfDisabled(state, "remove");
await ensureLoaded(state);
const before = state.store?.jobs.length ?? 0;
if (!state.store) return {
ok: false,
removed: false
};
state.store.jobs = state.store.jobs.filter((j) => j.id !== id);
const removed = (state.store.jobs.length ?? 0) !== before;
await persist(state);
armTimer(state);
if (removed) emit(state, {
jobId: id,
action: "removed"
});
return {
ok: true,
removed
};
});
}
let nextManualRunId = 1;
async function inspectManualRunPreflight(state, id, mode) {
return await locked(state, async () => {
warnIfDisabled(state, "run");
await ensureLoaded(state, { skipRecompute: true });
recomputeNextRunsForMaintenance(state);
const job = findJobOrThrow(state, id);
if (typeof job.state.runningAtMs === "number") return {
ok: true,
ran: false,
reason: "already-running"
};
const now = state.deps.nowMs();
if (!isJobDue(job, now, { forced: mode === "force" })) return {
ok: true,
ran: false,
reason: "not-due"
};
return {
ok: true,
runnable: true,
job,
now
};
});
}
async function inspectManualRunDisposition(state, id, mode) {
const result = await inspectManualRunPreflight(state, id, mode);
if (!result.ok) return result;
if ("reason" in result) return result;
return {
ok: true,
runnable: true
};
}
async function prepareManualRun(state, id, mode) {
const preflight = await inspectManualRunPreflight(state, id, mode);
if (!preflight.ok) return preflight;
if ("reason" in preflight) return {
ok: true,
ran: false,
reason: preflight.reason
};
return await locked(state, async () => {
const job = findJobOrThrow(state, id);
if (typeof job.state.runningAtMs === "number") return {
ok: true,
ran: false,
reason: "already-running"
};
job.state.runningAtMs = preflight.now;
job.state.lastError = void 0;
await persist(state);
emit(state, {
jobId: job.id,
action: "started",
runAtMs: preflight.now
});
const executionJob = JSON.parse(JSON.stringify(job));
return {
ok: true,
ran: true,
jobId: job.id,
startedAt: preflight.now,
executionJob
};
});
}
async function finishPreparedManualRun(state, prepared, mode) {
const executionJob = prepared.executionJob;
const startedAt = prepared.startedAt;
const jobId = prepared.jobId;
let coreResult;
try {
coreResult = await executeJobCoreWithTimeout(state, executionJob);
} catch (err) {
coreResult = {
status: "error",
error: String(err)
};
}
const endedAt = state.deps.nowMs();
await locked(state, async () => {
await ensureLoaded(state, { skipRecompute: true });
const job = state.store?.jobs.find((entry) => entry.id === jobId);
if (!job) return;
const shouldDelete = applyJobResult(state, job, {
status: coreResult.status,
error: coreResult.error,
delivered: coreResult.delivered,
startedAt,
endedAt
}, { preserveSchedule: mode === "force" });
emit(state, {
jobId: job.id,
action: "finished",
status: coreResult.status,
error: coreResult.error,
summary: coreResult.summary,
delivered: coreResult.delivered,
deliveryStatus: job.state.lastDeliveryStatus,
deliveryError: job.state.lastDeliveryError,
sessionId: coreResult.sessionId,
sessionKey: coreResult.sessionKey,
runAtMs: startedAt,
durationMs: job.state.lastDurationMs,
nextRunAtMs: job.state.nextRunAtMs,
model: coreResult.model,
provider: coreResult.provider,
usage: coreResult.usage
});
if (shouldDelete && state.store) {
state.store.jobs = state.store.jobs.filter((entry) => entry.id !== job.id);
emit(state, {
jobId: job.id,
action: "removed"
});
}
const postRunSnapshot = shouldDelete ? null : {
enabled: job.enabled,
updatedAtMs: job.updatedAtMs,
state: structuredClone(job.state)
};
const postRunRemoved = shouldDelete;
await ensureLoaded(state, {
forceReload: true,
skipRecompute: true
});
mergeManualRunSnapshotAfterReload({
state,
jobId,
snapshot: postRunSnapshot,
removed: postRunRemoved
});
recomputeNextRunsForMaintenance(state, { recomputeExpired: true });
await persist(state);
armTimer(state);
});
}
async function run(state, id, mode) {
const prepared = await prepareManualRun(state, id, mode);
if (!prepared.ok || !prepared.ran) return prepared;
await finishPreparedManualRun(state, prepared, mode);
return {
ok: true,
ran: true
};
}
async function enqueueRun(state, id, mode) {
const disposition = await inspectManualRunDisposition(state, id, mode);
if (!disposition.ok || !("runnable" in disposition && disposition.runnable)) return disposition;
const runId = `manual:${id}:${state.deps.nowMs()}:${nextManualRunId++}`;
enqueueCommandInLane(CommandLane.Cron, async () => {
const result = await run(state, id, mode);
if (result.ok && "ran" in result && !result.ran) state.deps.log.info({
jobId: id,
runId,
reason: result.reason
}, "cron: queued manual run skipped before execution");
return result;
}, {
warnAfterMs: 5e3,
onWait: (waitMs, queuedAhead) => {
state.deps.log.warn({
jobId: id,
runId,
waitMs,
queuedAhead
}, "cron: queued manual run waiting for an execution slot");
}
}).catch((err) => {
state.deps.log.error({
jobId: id,
runId,
err: String(err)
}, "cron: queued manual run background execution failed");
});
return {
ok: true,
enqueued: true,
runId
};
}
function wakeNow(state, opts) {
return wake(state, opts);
}
//#endregion
//#region src/cron/service/state.ts
function createCronServiceState(deps) {
return {
deps: {
...deps,
nowMs: deps.nowMs ?? (() => Date.now())
},
store: null,
timer: null,
running: false,
op: Promise.resolve(),
warnedDisabled: false,
storeLoadedAtMs: null,
storeFileMtimeMs: null
};
}
//#endregion
//#region src/cron/service.ts
var CronService = class {
constructor(deps) {
this.state = createCronServiceState(deps);
}
async start() {
await start(this.state);
}
stop() {
stop(this.state);
}
async status() {
return await status(this.state);
}
async list(opts) {
return await list(this.state, opts);
}
async listPage(opts) {
return await listPage(this.state, opts);
}
async add(input) {
return await add(this.state, input);
}
async update(id, patch) {
return await update(this.state, id, patch);
}
async remove(id) {
return await remove(this.state, id);
}
async run(id, mode) {
return await run(this.state, id, mode);
}
async enqueueRun(id, mode) {
return await enqueueRun(this.state, id, mode);
}
getJob(id) {
return this.state.store?.jobs.find((job) => job.id === id);
}
wake(opts) {
return wakeNow(this.state, opts);
}
};
//#endregion
//#region src/gateway/server-cron.ts
const CRON_WEBHOOK_TIMEOUT_MS = 1e4;
function trimToOptionalString(value) {
if (typeof value !== "string") return;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : void 0;
}
function redactWebhookUrl(url) {
try {
const parsed = new URL(url);
return `${parsed.origin}${parsed.pathname}`;
} catch {
return "<invalid-webhook-url>";
}
}
function resolveCronWebhookTarget(params) {
if (params.delivery?.mode?.trim().toLowerCase() === "webhook") {
const url = normalizeHttpWebhookUrl(params.delivery?.to);
return url ? {
url,
source: "delivery"
} : null;
}
if (params.legacyNotify) {
const legacyUrl = normalizeHttpWebhookUrl(params.legacyWebhook);
if (legacyUrl) return {
url: legacyUrl,
source: "legacy"
};
}
return null;
}
function buildCronWebhookHeaders(webhookToken) {
const headers = { "Content-Type": "application/json" };
if (webhookToken) headers.Authorization = `Bearer ${webhookToken}`;
return headers;
}
async function postCronWebhook(params) {
const abortController = new AbortController();
const timeout = setTimeout(() => {
abortController.abort();
}, CRON_WEBHOOK_TIMEOUT_MS);
try {
await (await fetchWithSsrFGuard({
url: params.webhookUrl,
init: {
method: "POST",
headers: buildCronWebhookHeaders(params.webhookToken),
body: JSON.stringify(params.payload),
signal: abortController.signal
}
})).release();
} catch (err) {
if (err instanceof SsrFBlockedError) params.logger.warn({
...params.logContext,
reason: formatErrorMessage(err),
webhookUrl: redactWebhookUrl(params.webhookUrl)
}, params.blockedLog);
else params.logger.warn({
...params.logContext,
err: formatErrorMessage(err),
webhookUrl: redactWebhookUrl(params.webhookUrl)
}, params.failedLog);
} finally {
clearTimeout(timeout);
}
}
function buildGatewayCronService(params) {
const cronLogger = getChildLogger({ module: "cron" });
const storePath = resolveCronStorePath(params.cfg.cron?.store);
const cronEnabled = process.env.OPENCLAW_SKIP_CRON !== "1" && params.cfg.cron?.enabled !== false;
const resolveCronAgent = (requested) => {
const runtimeConfig = loadConfig();
const normalized = typeof requested === "string" && requested.trim() ? normalizeAgentId(requested) : void 0;
return {
agentId: normalized !== void 0 && Array.isArray(runtimeConfig.agents?.list) && runtimeConfig.agents.list.some((entry) => entry && typeof entry.id === "string" && normalizeAgentId(entry.id) === normalized) ? normalized : resolveDefaultAgentId(runtimeConfig),
cfg: runtimeConfig
};
};
const resolveCronSessionKey = (params) => {
const requested = params.requestedSessionKey?.trim();
if (!requested) return resolveAgentMainSessionKey({
cfg: params.runtimeConfig,
agentId: params.agentId
});
const candidate = toAgentStoreSessionKey({
agentId: params.agentId,
requestKey: requested,
mainKey: params.runtimeConfig.session?.mainKey
});
const canonical = canonicalizeMainSessionAlias({
cfg: params.runtimeConfig,
agentId: params.agentId,
sessionKey: candidate
});
if (canonical !== "global") {
if (normalizeAgentId(resolveAgentIdFromSessionKey(canonical)) !== normalizeAgentId(params.agentId)) return resolveAgentMainSessionKey({
cfg: params.runtimeConfig,
agentId: params.agentId
});
}
return canonical;
};
const resolveCronWakeTarget = (opts) => {
const runtimeConfig = loadConfig();
const agentId = ((opts?.agentId ? resolveCronAgent(opts.agentId).agentId : void 0) ?? (opts?.sessionKey ? normalizeAgentId(resolveAgentIdFromSessionKey(opts.sessionKey)) : void 0)) || void 0;
return {
runtimeConfig,
agentId,
sessionKey: opts?.sessionKey && agentId ? resolveCronSessionKey({
runtimeConfig,
agentId,
requestedSessionKey: opts.sessionKey
}) : void 0
};
};
const defaultAgentId = resolveDefaultAgentId(params.cfg);
const runLogPrune = resolveCronRunLogPruneOptions(params.cfg.cron?.runLog);
const resolveSessionStorePath = (agentId) => resolveStorePath(params.cfg.session?.store, { agentId: agentId ?? defaultAgentId });
const sessionStorePath = resolveSessionStorePath(defaultAgentId);
const warnedLegacyWebhookJobs = /* @__PURE__ */ new Set();
const cron = new CronService({
storePath,
cronEnabled,
cronConfig: params.cfg.cron,
defaultAgentId,
resolveSessionStorePath,
sessionStorePath,
enqueueSystemEvent: (text, opts) => {
const { agentId, cfg: runtimeConfig } = resolveCronAgent(opts?.agentId);
enqueueSystemEvent(text, {
sessionKey: resolveCronSessionKey({
runtimeConfig,
agentId,
requestedSessionKey: opts?.sessionKey
}),
contextKey: opts?.contextKey
});
},
requestHeartbeatNow: (opts) => {
const { agentId, sessionKey } = resolveCronWakeTarget(opts);
requestHeartbeatNow({
reason: opts?.reason,
agentId,
sessionKey
});
},
runHeartbeatOnce: async (opts) => {
const { runtimeConfig, agentId, sessionKey } = resolveCronWakeTarget(opts);
const agentEntry = Array.isArray(runtimeConfig.agents?.list) && runtimeConfig.agents.list.find((entry) => entry && typeof entry.id === "string" && normalizeAgentId(entry.id) === agentId);
const agentHeartbeat = agentEntry && typeof agentEntry === "object" ? agentEntry.heartbeat : void 0;
const baseHeartbeat = {
...runtimeConfig.agents?.defaults?.heartbeat,
...agentHeartbeat
};
const heartbeatOverride = opts?.heartbeat ? {
...baseHeartbeat,
...opts.heartbeat
} : void 0;
return await runHeartbeatOnce({
cfg: runtimeConfig,
reason: opts?.reason,
agentId,
sessionKey,
heartbeat: heartbeatOverride,
deps: {
...params.deps,
runtime: defaultRuntime
}
});
},
runIsolatedAgentJob: async ({ job, message, abortSignal }) => {
const { agentId, cfg: runtimeConfig } = resolveCronAgent(job.agentId);
let sessionKey = `cron:${job.id}`;
if (job.sessionTarget.startsWith("session:")) {
const customSessionId = job.sessionTarget.slice(8).trim();
if (customSessionId) sessionKey = customSessionId;
}
return await runCronIsolatedAgentTurn({
cfg: runtimeConfig,
deps: params.deps,
job,
message,
abortSignal,
agentId,
sessionKey,
lane: "cron"
});
},
sendCronFailureAlert: async ({ job, text, channel, to, mode, accountId }) => {
const { agentId, cfg: runtimeConfig } = resolveCronAgent(job.agentId);
const webhookToken = trimToOptionalString(params.cfg.cron?.webhookToken);
if (mode === "webhook" && !to) {
cronLogger.warn({ jobId: job.id }, "cron: failure alert webhook mode requires URL, skipping");
return;
}
if (mode === "webhook" && to) {
const webhookUrl = normalizeHttpWebhookUrl(to);
if (webhookUrl) await postCronWebhook({
webhookUrl,
webhookToken,
payload: {
jobId: job.id,
jobName: job.name,
message: text
},
logContext: { jobId: job.id },
blockedLog: "cron: failure alert webhook blocked by SSRF guard",
failedLog: "cron: failure alert webhook failed",
logger: cronLogger
});
else cronLogger.warn({
jobId: job.id,
webhookUrl: redactWebhookUrl(to)
}, "cron: failure alert webhook URL is invalid, skipping");
return;
}
const target = await resolveDeliveryTarget(runtimeConfig, agentId, {
channel,
to,
accountId
});
if (!target.ok) throw target.error;
await deliverOutboundPayloads({
cfg: runtimeConfig,
channel: target.channel,
to: target.to,
accountId: target.accountId,
threadId: target.threadId,
payloads: [{ text }],
deps: createOutboundSendDeps(params.deps)
});
},
log: getChildLogger({
module: "cron",
storePath
}),
onEvent: (evt) => {
params.broadcast("cron", evt, { dropIfSlow: true });
if (evt.action === "finished") {
const webhookToken = trimToOptionalString(params.cfg.cron?.webhookToken);
const legacyWebhook = trimToOptionalString(params.cfg.cron?.webhook);
const job = cron.getJob(evt.jobId);
const legacyNotify = job?.notify === true;
const webhookTarget = resolveCronWebhookTarget({
delivery: job?.delivery && typeof job.delivery.mode === "string" ? {
mode: job.delivery.mode,
to: job.delivery.to
} : void 0,
legacyNotify,
legacyWebhook
});
if (!webhookTarget && job?.delivery?.mode === "webhook") cronLogger.warn({
jobId: evt.jobId,
deliveryTo: job.delivery.to
}, "cron: skipped webhook delivery, delivery.to must be a valid http(s) URL");
if (webhookTarget?.source === "legacy" && !warnedLegacyWebhookJobs.has(evt.jobId)) {
warnedLegacyWebhookJobs.add(evt.jobId);
cronLogger.warn({
jobId: evt.jobId,
legacyWebhook: redactWebhookUrl(webhookTarget.url)
}, "cron: deprecated notify+cron.webhook fallback in use, migrate to delivery.mode=webhook with delivery.to");
}
if (webhookTarget && evt.summary) (async () => {
await postCronWebhook({
webhookUrl: webhookTarget.url,
webhookToken,
payload: evt,
logContext: { jobId: evt.jobId },
blockedLog: "cron: webhook delivery blocked by SSRF guard",
failedLog: "cron: webhook delivery failed",
logger: cronLogger
});
})();
if (evt.status === "error" && job) {
const failureDest = resolveFailureDestination(job, params.cfg.cron?.failureDestination);
if (failureDest) {
if (!(job.delivery?.bestEffort === true || job.payload.kind === "agentTurn" && job.payload.bestEffortDeliver === true)) {
const failureMessage = `Cron job "${job.name}" failed: ${evt.error ?? "unknown error"}`;
const failurePayload = {
jobId: job.id,
jobName: job.name,
message: failureMessage,
status: evt.status,
error: evt.error,
runAtMs: evt.runAtMs,
durationMs: evt.durationMs,
nextRunAtMs: evt.nextRunAtMs
};
if (failureDest.mode === "webhook" && failureDest.to) {
const webhookUrl = normalizeHttpWebhookUrl(failureDest.to);
if (webhookUrl) (async () => {
await postCronWebhook({
webhookUrl,
webhookToken,
payload: failurePayload,
logContext: { jobId: evt.jobId },
blockedLog: "cron: failure destination webhook blocked by SSRF guard",
failedLog: "cron: failure destination webhook failed",
logger: cronLogger
});
})();
else cronLogger.warn({
jobId: evt.jobId,
webhookUrl: redactWebhookUrl(failureDest.to)
}, "cron: failure destination webhook URL is invalid, skipping");
} else if (failureDest.mode === "announce") {
const { agentId, cfg: runtimeConfig } = resolveCronAgent(job.agentId);
sendFailureNotificationAnnounce(params.deps, runtimeConfig, agentId, job.id, {
channel: failureDest.channel,
to: failureDest.to,
accountId: failureDest.accountId
}, `[Cron Failure] ${failureMessage}`);
}
}
}
}
const logPath = resolveCronRunLogPath({
storePath,
jobId: evt.jobId
});
appendCronRunLog(logPath, {
ts: Date.now(),
jobId: evt.jobId,
action: "finished",
status: evt.status,
error: evt.error,
summary: evt.summary,
delivered: evt.delivered,
deliveryStatus: evt.deliveryStatus,
deliveryError: evt.deliveryError,
sessionId: evt.sessionId,
sessionKey: evt.sessionKey,
runAtMs: evt.runAtMs,
durationMs: evt.durationMs,
nextRunAtMs: evt.nextRunAtMs,
model: evt.model,
provider: evt.provider,
usage: evt.usage
}, runLogPrune).catch((err) => {
cronLogger.warn({
err: String(err),
logPath
}, "cron: run log append failed");
});
}
}
});
return {
cron,
storePath,
cronEnabled
};
}
//#endregion
//#region src/infra/bonjour-errors.ts
function formatBonjourError(err) {
if (err instanceof Error) {
const msg = err.message.trim() || err.name || String(err).trim();
if (err.name && err.name !== "Error") return msg === err.name ? err.name : `${err.name}: ${msg}`;
return msg;
}
return String(err);
}
//#endregion
//#region src/infra/bonjour-ciao.ts
const CIAO_CANCELLATION_MESSAGE_RE = /^CIAO (?:ANNOUNCEMENT|PROBING) CANCELLED\b/u;
const CIAO_INTERFACE_ASSERTION_MESSAGE_RE = /REACHED ILLEGAL STATE!?\s+IPV4 ADDRESS CHANGE FROM DEFINED TO UNDEFINED!?/u;
function classifyCiaoUnhandledRejection(reason) {
const formatted = formatBonjourError(reason);
const message = formatted.toUpperCase();
if (CIAO_CANCELLATION_MESSAGE_RE.test(message)) return {
kind: "cancellation",
formatted
};
if (CIAO_INTERFACE_ASSERTION_MESSAGE_RE.test(message)) return {
kind: "interface-assertion",
formatted
};
return null;
}
//#endregion
//#region src/infra/bonjour.ts
function isDisabledByEnv$1() {
if (isTruthyEnvValue(process.env.OPENCLAW_DISABLE_BONJOUR)) return true;
if (process.env.VITEST) return true;
return false;
}
function safeServiceName(name) {
const trimmed = name.trim();
return trimmed.length > 0 ? trimmed : "OpenClaw";
}
function prettifyInstanceName(name) {
const normalized = name.trim().replace(/\s+/g, " ");
return normalized.replace(/\s+\(OpenClaw\)\s*$/i, "").trim() || normalized;
}
const WATCHDOG_INTERVAL_MS = 5e3;
const REPAIR_DEBOUNCE_MS = 3e4;
const STUCK_ANNOUNCING_MS = 8e3;
function serviceSummary(label, svc) {
let fqdn = "unknown";
let hostname = "unknown";
let port = -1;
try {
fqdn = svc.getFQDN();
} catch {}
try {
hostname = svc.getHostname();
} catch {}
try {
port = svc.getPort();
} catch {}
const state = typeof svc.serviceState === "string" ? svc.serviceState : "unknown";
return `${label} fqdn=${fqdn} host=${hostname} port=${port} state=${state}`;
}
function isAnnouncedState(state) {
return String(state) === "announced";
}
function handleCiaoUnhandledRejection(reason) {
const classification = classifyCiaoUnhandledRejection(reason);
if (!classification) return false;
if (classification.kind === "interface-assertion") {
logWarn(`bonjour: suppressing ciao interface assertion: ${classification.formatted}`);
return true;
}
logDebug(`bonjour: ignoring unhandled ciao rejection: ${classification.formatted}`);
return true;
}
async function startGatewayBonjourAdvertiser(opts) {
if (isDisabledByEnv$1()) return { stop: async () => {} };
const { getResponder, Protocol } = await import("@homebridge/ciao");
const hostname = (process.env.OPENCLAW_MDNS_HOSTNAME?.trim() || "openclaw").replace(/\.local$/i, "").split(".")[0].trim() || "openclaw";
const instanceName = typeof opts.instanceName === "string" && opts.instanceName.trim() ? opts.instanceName.trim() : `${hostname} (OpenClaw)`;
const displayName = prettifyInstanceName(instanceName);
const txtBase = {
role: "gateway",
gatewayPort: String(opts.gatewayPort),
lanHost: `${hostname}.local`,
displayName
};
if (opts.gatewayTlsEnabled) {
txtBase.gatewayTls = "1";
if (opts.gatewayTlsFingerprintSha256) txtBase.gatewayTlsSha256 = opts.gatewayTlsFingerprintSha256;
}
if (typeof opts.canvasPort === "number" && opts.canvasPort > 0) txtBase.canvasPort = String(opts.canvasPort);
if (typeof opts.tailnetDns === "string" && opts.tailnetDns.trim()) txtBase.tailnetDns = opts.tailnetDns.trim();
if (!opts.minimal && typeof opts.cliPath === "string" && opts.cliPath.trim()) txtBase.cliPath = opts.cliPath.trim();
const gatewayTxt = {
...txtBase,
transport: "gateway"
};
if (!opts.minimal) gatewayTxt.sshPort = String(opts.sshPort ?? 22);
function createCycle() {
const responder = getResponder();
const services = [];
const gateway = responder.createService({
name: safeServiceName(instanceName),
type: "openclaw-gw",
protocol: Protocol.TCP,
port: opts.gatewayPort,
domain: "local",
hostname,
txt: gatewayTxt
});
services.push({
label: "gateway",
svc: gateway
});
return {
responder,
services,
cleanupUnhandledRejection: services.length > 0 ? registerUnhandledRejectionHandler(handleCiaoUnhandledRejection) : void 0
};
}
async function stopCycle(cycle) {
if (!cycle) return;
const responder = cycle.responder;
const noopAsync = async () => {};
responder.advertiseService = noopAsync;
responder.announce = noopAsync;
responder.probe = noopAsync;
responder.republishService = noopAsync;
for (const { svc } of cycle.services) try {
await svc.destroy();
} catch {}
try {
await cycle.responder.shutdown();
} catch {} finally {
cycle.cleanupUnhandledRejection?.();
}
}
function attachConflictListeners(services) {
for (const { label, svc } of services) try {
svc.on("name-change", (name) => {
const next = typeof name === "string" ? name : String(name);
logWarn(`bonjour: ${label} name conflict resolved; newName=${JSON.stringify(next)}`);
});
svc.on("hostname-change", (nextHostname) => {
const next = typeof nextHostname === "string" ? nextHostname : String(nextHostname);
logWarn(`bonjour: ${label} hostname conflict resolved; newHostname=${JSON.stringify(next)}`);
});
} catch (err) {
logDebug(`bonjour: failed to attach listeners for ${label}: ${String(err)}`);
}
}
function startAdvertising(services) {
for (const { label, svc } of services) try {
svc.advertise().then(() => {
getLogger().info(`bonjour: advertised ${serviceSummary(label, svc)}`);
}).catch((err) => {
logWarn(`bonjour: advertise failed (${serviceSummary(label, svc)}): ${formatBonjourError(err)}`);
});
} catch (err) {
logWarn(`bonjour: advertise threw (${serviceSummary(label, svc)}): ${formatBonjourError(err)}`);
}
}
logDebug(`bonjour: starting (hostname=${hostname}, instance=${JSON.stringify(safeServiceName(instanceName))}, gatewayPort=${opts.gatewayPort}${opts.minimal ? ", minimal=true" : `, sshPort=${opts.sshPort ?? 22}`})`);
let stopped = false;
let recreatePromise = null;
let cycle = createCycle();
const stateTracker = /* @__PURE__ */ new Map();
attachConflictListeners(cycle.services);
startAdvertising(cycle.services);
const updateStateTrackers = (services) => {
const now = Date.now();
for (const { label, svc } of services) {
const nextState = typeof svc.serviceState === "string" ? svc.serviceState : "unknown";
const current = stateTracker.get(label);
const nextEnteredAt = current && !isAnnouncedState(current.state) && !isAnnouncedState(nextState) ? current.sinceMs : now;
if (!current || current.state !== nextState || current.sinceMs !== nextEnteredAt) stateTracker.set(label, {
state: nextState,
sinceMs: nextEnteredAt
});
}
};
const recreateAdvertiser = async (reason) => {
if (stopped) return;
if (recreatePromise) return recreatePromise;
recreatePromise = (async () => {
logWarn(`bonjour: restarting advertiser (${reason})`);
await stopCycle(cycle);
cycle = createCycle();
stateTracker.clear();
attachConflictListeners(cycle.services);
startAdvertising(cycle.services);
})().finally(() => {
recreatePromise = null;
});
return recreatePromise;
};
const lastRepairAttempt = /* @__PURE__ */ new Map();
const watchdog = setInterval(() => {
if (stopped || recreatePromise) return;
updateStateTrackers(cycle.services);
for (const { label, svc } of cycle.services) {
const stateUnknown = svc.serviceState;
if (typeof stateUnknown !== "string") continue;
const tracked = stateTracker.get(label);
if (stateUnknown !== "announced" && tracked && Date.now() - tracked.sinceMs >= STUCK_ANNOUNCING_MS) {
recreateAdvertiser(`service stuck in ${stateUnknown} for ${Date.now() - tracked.sinceMs}ms (${serviceSummary(label, svc)})`);
return;
}
if (stateUnknown === "announced" || stateUnknown === "announcing") continue;
let key = label;
try {
key = `${label}:${svc.getFQDN()}`;
} catch {}
const now = Date.now();
if (now - (lastRepairAttempt.get(key) ?? 0) < REPAIR_DEBOUNCE_MS) continue;
lastRepairAttempt.set(key, now);
logWarn(`bonjour: watchdog detected non-announced service; attempting re-advertise (${serviceSummary(label, svc)})`);
try {
svc.advertise().catch((err) => {
logWarn(`bonjour: watchdog advertise failed (${serviceSummary(label, svc)}): ${formatBonjourError(err)}`);
});
} catch (err) {
logWarn(`bonjour: watchdog advertise threw (${serviceSummary(label, svc)}): ${formatBonjourError(err)}`);
}
}
}, WATCHDOG_INTERVAL_MS);
watchdog.unref?.();
return { stop: async () => {
stopped = true;
clearInterval(watchdog);
await recreatePromise;
await stopCycle(cycle);
} };
}
//#endregion
//#region src/gateway/server-discovery.ts
function formatBonjourInstanceName(displayName) {
const trimmed = displayName.trim();
if (!trimmed) return "OpenClaw";
if (/openclaw/i.test(trimmed)) return trimmed;
return `${trimmed} (OpenClaw)`;
}
function resolveBonjourCliPath(opts = {}) {
const envPath = (opts.env ?? process.env).OPENCLAW_CLI_PATH?.trim();
if (envPath) return envPath;
const statSync = opts.statSync ?? fs.statSync;
const isFile = (candidate) => {
try {
return statSync(candidate).isFile();
} catch {
return false;
}
};
const execPath = opts.execPath ?? process.execPath;
const execDir = path.dirname(execPath);
const siblingCli = path.join(execDir, "openclaw");
if (isFile(siblingCli)) return siblingCli;
const argvPath = (opts.argv ?? process.argv)[1];
if (argvPath && isFile(argvPath)) return argvPath;
const cwd = opts.cwd ?? process.cwd();
const distCli = path.join(cwd, "dist", "index.js");
if (isFile(distCli)) return distCli;
const binCli = path.join(cwd, "bin", "openclaw");
if (isFile(binCli)) return binCli;
}
async function resolveTailnetDnsHint(opts) {
const envRaw = (opts?.env ?? process.env).OPENCLAW_TAILNET_DNS?.trim();
const envValue = envRaw && envRaw.length > 0 ? envRaw.replace(/\.$/, "") : "";
if (envValue) return envValue;
if (opts?.enabled === false) return;
const exec = opts?.exec ?? ((command, args) => runExec(command, args, {
timeoutMs: 1500,
maxBuffer: 2e5
}));
try {
return await getTailnetHostname(exec);
} catch {
return;
}
}
//#endregion
//#region src/gateway/server-discovery-runtime.ts
async function startGatewayDiscovery(params) {
let bonjourStop = null;
const mdnsMode = params.mdnsMode ?? "minimal";
const bonjourEnabled = mdnsMode !== "off" && process.env.OPENCLAW_DISABLE_BONJOUR !== "1" && !process.env.VITEST;
const mdnsMinimal = mdnsMode !== "full";
const tailscaleEnabled = params.tailscaleMode !== "off";
const tailnetDns = bonjourEnabled || params.wideAreaDiscoveryEnabled ? await resolveTailnetDnsHint({ enabled: tailscaleEnabled }) : void 0;
const sshPortEnv = mdnsMinimal ? void 0 : process.env.OPENCLAW_SSH_PORT?.trim();
const sshPortParsed = sshPortEnv ? Number.parseInt(sshPortEnv, 10) : NaN;
const sshPort = Number.isFinite(sshPortParsed) && sshPortParsed > 0 ? sshPortParsed : void 0;
const cliPath = mdnsMinimal ? void 0 : resolveBonjourCliPath();
if (bonjourEnabled) try {
bonjourStop = (await startGatewayBonjourAdvertiser({
instanceName: formatBonjourInstanceName(params.machineDisplayName),
gatewayPort: params.port,
gatewayTlsEnabled: params.gatewayTls?.enabled ?? false,
gatewayTlsFingerprintSha256: params.gatewayTls?.fingerprintSha256,
canvasPort: params.canvasPort,
sshPort,
tailnetDns,
cliPath,
minimal: mdnsMinimal
})).stop;
} catch (err) {
params.logDiscovery.warn(`bonjour advertising failed: ${String(err)}`);
}
if (params.wideAreaDiscoveryEnabled) {
const wideAreaDomain = resolveWideAreaDiscoveryDomain({ configDomain: params.wideAreaDiscoveryDomain ?? void 0 });
if (!wideAreaDomain) {
params.logDiscovery.warn("discovery.wideArea.enabled is true, but no domain was configured; set discovery.wideArea.domain to enable unicast DNS-SD");
return { bonjourStop };
}
const tailnetIPv4 = pickPrimaryTailnetIPv4();
if (!tailnetIPv4) params.logDiscovery.warn("discovery.wideArea.enabled is true, but no Tailscale IPv4 address was found; skipping unicast DNS-SD zone update");
else try {
const tailnetIPv6 = pickPrimaryTailnetIPv6();
const result = await writeWideAreaGatewayZone({
domain: wideAreaDomain,
gatewayPort: params.port,
displayName: formatBonjourInstanceName(params.machineDisplayName),
tailnetIPv4,
tailnetIPv6: tailnetIPv6 ?? void 0,
gatewayTlsEnabled: params.gatewayTls?.enabled ?? false,
gatewayTlsFingerprintSha256: params.gatewayTls?.fingerprintSha256,
tailnetDns,
sshPort,
cliPath: resolveBonjourCliPath()
});
params.logDiscovery.info(`wide-area DNS-SD ${result.changed ? "updated" : "unchanged"} (${wideAreaDomain} → ${result.zonePath})`);
} catch (err) {
params.logDiscovery.warn(`wide-area discovery update failed: ${String(err)}`);
}
}
return { bonjourStop };
}
//#endregion
//#region src/gateway/server-lanes.ts
function applyGatewayLaneConcurrency(cfg) {
setCommandLaneConcurrency(CommandLane.Cron, cfg.cron?.maxConcurrentRuns ?? 1);
setCommandLaneConcurrency(CommandLane.Main, resolveAgentMaxConcurrent(cfg));
setCommandLaneConcurrency(CommandLane.Subagent, resolveSubagentMaxConcurrent(cfg));
}
//#endregion
//#region src/gateway/chat-abort.ts
function isChatStopCommandText(text) {
return isAbortRequestText(text);
}
function resolveChatRunExpiresAtMs(params) {
const { now, timeoutMs, graceMs = 6e4, minMs = 2 * 6e4, maxMs = 1440 * 6e4 } = params;
const target = now + Math.max(0, timeoutMs) + graceMs;
const min = now + minMs;
const max = now + maxMs;
return Math.min(max, Math.max(min, target));
}
function broadcastChatAborted(ops, params) {
const { runId, sessionKey, stopReason, partialText } = params;
const payload = {
runId,
sessionKey,
seq: (ops.agentRunSeq.get(runId) ?? 0) + 1,
state: "aborted",
stopReason,
message: partialText ? {
role: "assistant",
content: [{
type: "text",
text: partialText
}],
timestamp: Date.now()
} : void 0
};
ops.broadcast("chat", payload);
ops.nodeSendToSession(sessionKey, "chat", payload);
}
function abortChatRunById(ops, params) {
const { runId, sessionKey, stopReason } = params;
const active = ops.chatAbortControllers.get(runId);
if (!active) return { aborted: false };
if (active.sessionKey !== sessionKey) return { aborted: false };
const bufferedText = ops.chatRunBuffers.get(runId);
const partialText = bufferedText && bufferedText.trim() ? bufferedText : void 0;
ops.chatAbortedRuns.set(runId, Date.now());
active.controller.abort();
ops.chatAbortControllers.delete(runId);
ops.chatRunBuffers.delete(runId);
ops.chatDeltaSentAt.delete(runId);
ops.chatDeltaLastBroadcastLen.delete(runId);
const removed = ops.removeChatRun(runId, runId, sessionKey);
broadcastChatAborted(ops, {
runId,
sessionKey,
stopReason,
partialText
});
ops.agentRunSeq.delete(runId);
if (removed?.clientRunId) ops.agentRunSeq.delete(removed.clientRunId);
return { aborted: true };
}
//#endregion
//#region src/gateway/server-constants.ts
const MAX_PAYLOAD_BYTES = 25 * 1024 * 1024;
const MAX_BUFFERED_BYTES = 50 * 1024 * 1024;
const MAX_PREAUTH_PAYLOAD_BYTES = 64 * 1024;
let maxChatHistoryMessagesBytes = 6 * 1024 * 1024;
const getMaxChatHistoryMessagesBytes = () => maxChatHistoryMessagesBytes;
const DEFAULT_HANDSHAKE_TIMEOUT_MS = 1e4;
const getHandshakeTimeoutMs = () => {
const envKey = process.env.OPENCLAW_HANDSHAKE_TIMEOUT_MS || process.env.VITEST && process.env.OPENCLAW_TEST_HANDSHAKE_TIMEOUT_MS;
if (envKey) {
const parsed = Number(envKey);
if (Number.isFinite(parsed) && parsed > 0) return parsed;
}
return DEFAULT_HANDSHAKE_TIMEOUT_MS;
};
const TICK_INTERVAL_MS = 3e4;
const HEALTH_REFRESH_INTERVAL_MS = 6e4;
const DEDUPE_TTL_MS = 5 * 6e4;
const DEDUPE_MAX = 1e3;
//#endregion
//#region src/infra/voicewake.ts
const DEFAULT_TRIGGERS = [
"openclaw",
"claude",
"computer"
];
function resolvePath$1(baseDir) {
const root = baseDir ?? resolveStateDir();
return path.join(root, "settings", "voicewake.json");
}
function sanitizeTriggers(triggers) {
const cleaned = (triggers ?? []).map((w) => typeof w === "string" ? w.trim() : "").filter((w) => w.length > 0);
return cleaned.length > 0 ? cleaned : DEFAULT_TRIGGERS;
}
const withLock = createAsyncLock();
function defaultVoiceWakeTriggers() {
return [...DEFAULT_TRIGGERS];
}
async function loadVoiceWakeConfig(baseDir) {
const existing = await readJsonFile(resolvePath$1(baseDir));
if (!existing) return {
triggers: defaultVoiceWakeTriggers(),
updatedAtMs: 0
};
return {
triggers: sanitizeTriggers(existing.triggers),
updatedAtMs: typeof existing.updatedAtMs === "number" && existing.updatedAtMs > 0 ? existing.updatedAtMs : 0
};
}
async function setVoiceWakeTriggers(triggers, baseDir) {
const sanitized = sanitizeTriggers(triggers);
const filePath = resolvePath$1(baseDir);
return await withLock(async () => {
const next = {
triggers: sanitized,
updatedAtMs: Date.now()
};
await writeJsonAtomic(filePath, next);
return next;
});
}
//#endregion
//#region src/gateway/server-utils.ts
function normalizeVoiceWakeTriggers(input) {
const cleaned = (Array.isArray(input) ? input : []).map((v) => typeof v === "string" ? v.trim() : "").filter((v) => v.length > 0).slice(0, 32).map((v) => v.slice(0, 64));
return cleaned.length > 0 ? cleaned : defaultVoiceWakeTriggers();
}
function formatError(err) {
if (err instanceof Error) return err.message;
if (typeof err === "string") return err;
const statusValue = err?.status;
const codeValue = err?.code;
if (statusValue !== void 0 || codeValue !== void 0) return `status=${typeof statusValue === "string" || typeof statusValue === "number" ? String(statusValue) : "unknown"} code=${typeof codeValue === "string" || typeof codeValue === "number" ? String(codeValue) : "unknown"}`;
try {
return JSON.stringify(err, null, 2);
} catch {
return String(err);
}
}
//#endregion
//#region src/infra/system-presence.ts
const entries = /* @__PURE__ */ new Map();
const TTL_MS = 300 * 1e3;
const MAX_ENTRIES = 200;
function normalizePresenceKey(key) {
if (!key) return;
const trimmed = key.trim();
if (!trimmed) return;
return trimmed.toLowerCase();
}
function resolvePrimaryIPv4() {
return pickBestEffortPrimaryLanIPv4() ?? os.hostname();
}
function initSelfPresence() {
const host = os.hostname();
const ip = resolvePrimaryIPv4() ?? void 0;
const version = resolveRuntimeServiceVersion(process.env);
const modelIdentifier = (() => {
if (os.platform() === "darwin") {
const res = spawnSync("sysctl", ["-n", "hw.model"], { encoding: "utf-8" });
const out = typeof res.stdout === "string" ? res.stdout.trim() : "";
return out.length > 0 ? out : void 0;
}
return os.arch();
})();
const macOSVersion = () => {
const res = spawnSync("sw_vers", ["-productVersion"], { encoding: "utf-8" });
const out = typeof res.stdout === "string" ? res.stdout.trim() : "";
return out.length > 0 ? out : os.release();
};
const selfEntry = {
host,
ip,
version,
platform: (() => {
const p = os.platform();
const rel = os.release();
if (p === "darwin") return `macos ${macOSVersion()}`;
if (p === "win32") return `windows ${rel}`;
return `${p} ${rel}`;
})(),
deviceFamily: (() => {
const p = os.platform();
if (p === "darwin") return "Mac";
if (p === "win32") return "Windows";
if (p === "linux") return "Linux";
return p;
})(),
modelIdentifier,
mode: "gateway",
reason: "self",
text: `Gateway: ${host}${ip ? ` (${ip})` : ""} · app ${version} · mode gateway · reason self`,
ts: Date.now()
};
const key = host.toLowerCase();
entries.set(key, selfEntry);
}
function ensureSelfPresence() {
if (entries.size === 0) initSelfPresence();
}
function touchSelfPresence() {
const key = os.hostname().toLowerCase();
const existing = entries.get(key);
if (existing) entries.set(key, {
...existing,
ts: Date.now()
});
else initSelfPresence();
}
initSelfPresence();
function parsePresence(text) {
const trimmed = text.trim();
const match = trimmed.match(/Node:\s*([^ (]+)\s*\(([^)]+)\)\s*·\s*app\s*([^·]+?)\s*·\s*last input\s*([0-9]+)s ago\s*·\s*mode\s*([^·]+?)\s*·\s*reason\s*(.+)$/i);
if (!match) return {
text: trimmed,
ts: Date.now()
};
const [, host, ip, version, lastInputStr, mode, reasonRaw] = match;
const lastInputSeconds = Number.parseInt(lastInputStr, 10);
const reason = reasonRaw.trim();
return {
host: host.trim(),
ip: ip.trim(),
version: version.trim(),
lastInputSeconds: Number.isFinite(lastInputSeconds) ? lastInputSeconds : void 0,
mode: mode.trim(),
reason,
text: trimmed,
ts: Date.now()
};
}
function mergeStringList(...values) {
const out = /* @__PURE__ */ new Set();
for (const list of values) {
if (!Array.isArray(list)) continue;
for (const item of list) {
const trimmed = String(item).trim();
if (trimmed) out.add(trimmed);
}
}
return out.size > 0 ? [...out] : void 0;
}
function updateSystemPresence(payload) {
ensureSelfPresence();
const parsed = parsePresence(payload.text);
const key = normalizePresenceKey(payload.deviceId) || normalizePresenceKey(payload.instanceId) || normalizePresenceKey(parsed.instanceId) || normalizePresenceKey(parsed.host) || parsed.ip || parsed.text.slice(0, 64) || os.hostname().toLowerCase();
const hadExisting = entries.has(key);
const existing = entries.get(key) ?? {};
const merged = {
...existing,
...parsed,
host: payload.host ?? parsed.host ?? existing.host,
ip: payload.ip ?? parsed.ip ?? existing.ip,
version: payload.version ?? parsed.version ?? existing.version,
platform: payload.platform ?? existing.platform,
deviceFamily: payload.deviceFamily ?? existing.deviceFamily,
modelIdentifier: payload.modelIdentifier ?? existing.modelIdentifier,
mode: payload.mode ?? parsed.mode ?? existing.mode,
lastInputSeconds: payload.lastInputSeconds ?? parsed.lastInputSeconds ?? existing.lastInputSeconds,
reason: payload.reason ?? parsed.reason ?? existing.reason,
deviceId: payload.deviceId ?? existing.deviceId,
roles: mergeStringList(existing.roles, payload.roles),
scopes: mergeStringList(existing.scopes, payload.scopes),
instanceId: payload.instanceId ?? parsed.instanceId ?? existing.instanceId,
text: payload.text || parsed.text || existing.text,
ts: Date.now()
};
entries.set(key, merged);
const trackKeys = [
"host",
"ip",
"version",
"mode",
"reason"
];
const changes = {};
const changedKeys = [];
for (const k of trackKeys) {
const prev = existing[k];
const next = merged[k];
if (prev !== next) {
changes[k] = next;
changedKeys.push(k);
}
}
return {
key,
previous: hadExisting ? existing : void 0,
next: merged,
changes,
changedKeys
};
}
function upsertPresence(key, presence) {
ensureSelfPresence();
const normalizedKey = normalizePresenceKey(key) ?? os.hostname().toLowerCase();
const existing = entries.get(normalizedKey) ?? {};
const roles = mergeStringList(existing.roles, presence.roles);
const scopes = mergeStringList(existing.scopes, presence.scopes);
const merged = {
...existing,
...presence,
roles,
scopes,
ts: Date.now(),
text: presence.text || existing.text || `Node: ${presence.host ?? existing.host ?? "unknown"} · mode ${presence.mode ?? existing.mode ?? "unknown"}`
};
entries.set(normalizedKey, merged);
}
function listSystemPresence() {
ensureSelfPresence();
const now = Date.now();
for (const [k, v] of entries) if (now - v.ts > TTL_MS) entries.delete(k);
if (entries.size > MAX_ENTRIES) {
const sorted = [...entries.entries()].toSorted((a, b) => a[1].ts - b[1].ts);
const toDrop = entries.size - MAX_ENTRIES;
for (let i = 0; i < toDrop; i++) entries.delete(sorted[i][0]);
}
touchSelfPresence();
return [...entries.values()].toSorted((a, b) => b.ts - a.ts);
}
//#endregion
//#region src/gateway/server/health-state.ts
let presenceVersion = 1;
let healthVersion = 1;
let healthCache = null;
let healthRefresh = null;
let broadcastHealthUpdate = null;
function buildGatewaySnapshot() {
const cfg = loadConfig();
const configPath = createConfigIO().configPath;
const defaultAgentId = resolveDefaultAgentId(cfg);
const mainKey = normalizeMainKey(cfg.session?.mainKey);
const mainSessionKey = resolveMainSessionKey(cfg);
const scope = cfg.session?.scope ?? "per-sender";
const presence = listSystemPresence();
const uptimeMs = Math.round(process.uptime() * 1e3);
const auth = resolveGatewayAuth({
authConfig: cfg.gateway?.auth,
env: process.env
});
const updateAvailable = getUpdateAvailable() ?? void 0;
return {
presence,
health: {},
stateVersion: {
presence: presenceVersion,
health: healthVersion
},
uptimeMs,
configPath,
stateDir: STATE_DIR,
sessionDefaults: {
defaultAgentId,
mainKey,
mainSessionKey,
scope
},
authMode: auth.mode,
updateAvailable
};
}
function getHealthCache() {
return healthCache;
}
function getHealthVersion() {
return healthVersion;
}
function incrementPresenceVersion() {
presenceVersion += 1;
return presenceVersion;
}
function getPresenceVersion() {
return presenceVersion;
}
function setBroadcastHealthUpdate(fn) {
broadcastHealthUpdate = fn;
}
async function refreshGatewayHealthSnapshot(opts) {
if (!healthRefresh) healthRefresh = (async () => {
const snap = await getHealthSnapshot({ probe: opts?.probe });
healthCache = snap;
healthVersion += 1;
if (broadcastHealthUpdate) broadcastHealthUpdate(snap);
return snap;
})().finally(() => {
healthRefresh = null;
});
return healthRefresh;
}
//#endregion
//#region src/gateway/server-maintenance.ts
function startGatewayMaintenanceTimers(params) {
setBroadcastHealthUpdate((snap) => {
params.broadcast("health", snap, { stateVersion: {
presence: params.getPresenceVersion(),
health: params.getHealthVersion()
} });
params.nodeSendToAllSubscribed("health", snap);
});
const tickInterval = setInterval(() => {
const payload = { ts: Date.now() };
params.broadcast("tick", payload, { dropIfSlow: true });
params.nodeSendToAllSubscribed("tick", payload);
}, TICK_INTERVAL_MS);
const healthInterval = setInterval(() => {
params.refreshGatewayHealthSnapshot({ probe: true }).catch((err) => params.logHealth.error(`refresh failed: ${formatError(err)}`));
}, HEALTH_REFRESH_INTERVAL_MS);
params.refreshGatewayHealthSnapshot({ probe: true }).catch((err) => params.logHealth.error(`initial refresh failed: ${formatError(err)}`));
const dedupeCleanup = setInterval(() => {
const AGENT_RUN_SEQ_MAX = 1e4;
const now = Date.now();
for (const [k, v] of params.dedupe) if (now - v.ts > 3e5) params.dedupe.delete(k);
if (params.dedupe.size > 1e3) {
const entries = [...params.dedupe.entries()].toSorted((a, b) => a[1].ts - b[1].ts);
for (let i = 0; i < params.dedupe.size - DEDUPE_MAX; i++) params.dedupe.delete(entries[i][0]);
}
if (params.agentRunSeq.size > AGENT_RUN_SEQ_MAX) {
const excess = params.agentRunSeq.size - AGENT_RUN_SEQ_MAX;
let removed = 0;
for (const runId of params.agentRunSeq.keys()) {
params.agentRunSeq.delete(runId);
removed += 1;
if (removed >= excess) break;
}
}
for (const [runId, entry] of params.chatAbortControllers) {
if (now <= entry.expiresAtMs) continue;
abortChatRunById({
chatAbortControllers: params.chatAbortControllers,
chatRunBuffers: params.chatRunBuffers,
chatDeltaSentAt: params.chatDeltaSentAt,
chatDeltaLastBroadcastLen: params.chatDeltaLastBroadcastLen,
chatAbortedRuns: params.chatRunState.abortedRuns,
removeChatRun: params.removeChatRun,
agentRunSeq: params.agentRunSeq,
broadcast: params.broadcast,
nodeSendToSession: params.nodeSendToSession
}, {
runId,
sessionKey: entry.sessionKey,
stopReason: "timeout"
});
}
const ABORTED_RUN_TTL_MS = 60 * 6e4;
for (const [runId, abortedAt] of params.chatRunState.abortedRuns) {
if (now - abortedAt <= ABORTED_RUN_TTL_MS) continue;
params.chatRunState.abortedRuns.delete(runId);
params.chatRunBuffers.delete(runId);
params.chatDeltaSentAt.delete(runId);
params.chatDeltaLastBroadcastLen.delete(runId);
}
for (const [runId, lastSentAt] of params.chatDeltaSentAt) {
if (params.chatRunState.abortedRuns.has(runId)) continue;
if (params.chatAbortControllers.has(runId)) continue;
if (now - lastSentAt <= ABORTED_RUN_TTL_MS) continue;
params.chatRunBuffers.delete(runId);
params.chatDeltaSentAt.delete(runId);
params.chatDeltaLastBroadcastLen.delete(runId);
}
}, 6e4);
if (typeof params.mediaCleanupTtlMs !== "number") return {
tickInterval,
healthInterval,
dedupeCleanup,
mediaCleanup: null
};
let mediaCleanupInFlight = null;
const runMediaCleanup = () => {
if (mediaCleanupInFlight) return mediaCleanupInFlight;
mediaCleanupInFlight = cleanOldMedia(params.mediaCleanupTtlMs, {
recursive: true,
pruneEmptyDirs: true
}).catch((err) => {
params.logHealth.error(`media cleanup failed: ${formatError(err)}`);
}).finally(() => {
mediaCleanupInFlight = null;
});
return mediaCleanupInFlight;
};
const mediaCleanup = setInterval(() => {
runMediaCleanup();
}, 60 * 6e4);
runMediaCleanup();
return {
tickInterval,
healthInterval,
dedupeCleanup,
mediaCleanup
};
}
//#endregion
//#region src/gateway/server-methods-list.ts
const BASE_METHODS = [
"health",
"doctor.memory.status",
"logs.tail",
"channels.status",
"channels.logout",
"status",
"usage.status",
"usage.cost",
"tts.status",
"tts.providers",
"tts.enable",
"tts.disable",
"tts.convert",
"tts.setProvider",
"config.get",
"config.set",
"config.apply",
"config.patch",
"config.schema",
"config.schema.lookup",
"exec.approvals.get",
"exec.approvals.set",
"exec.approvals.node.get",
"exec.approvals.node.set",
"exec.approval.request",
"exec.approval.waitDecision",
"exec.approval.resolve",
"wizard.start",
"wizard.next",
"wizard.cancel",
"wizard.status",
"talk.config",
"talk.speak",
"talk.mode",
"models.list",
"tools.catalog",
"agents.list",
"agents.create",
"agents.update",
"agents.delete",
"agents.files.list",
"agents.files.get",
"agents.files.set",
"skills.status",
"skills.bins",
"skills.install",
"skills.update",
"update.run",
"voicewake.get",
"voicewake.set",
"secrets.reload",
"secrets.resolve",
"sessions.list",
"sessions.subscribe",
"sessions.unsubscribe",
"sessions.messages.subscribe",
"sessions.messages.unsubscribe",
"sessions.preview",
"sessions.create",
"sessions.send",
"sessions.abort",
"sessions.patch",
"sessions.reset",
"sessions.delete",
"sessions.compact",
"last-heartbeat",
"set-heartbeats",
"wake",
"node.pair.request",
"node.pair.list",
"node.pair.approve",
"node.pair.reject",
"node.pair.verify",
"device.pair.list",
"device.pair.approve",
"device.pair.reject",
"device.pair.remove",
"device.token.rotate",
"device.token.revoke",
"node.rename",
"node.list",
"node.describe",
"node.pending.drain",
"node.pending.enqueue",
"node.invoke",
"node.pending.pull",
"node.pending.ack",
"node.invoke.result",
"node.event",
"node.canvas.capability.refresh",
"cron.list",
"cron.status",
"cron.add",
"cron.update",
"cron.remove",
"cron.run",
"cron.runs",
"gateway.identity.get",
"system-presence",
"system-event",
"send",
"agent",
"agent.identity.get",
"agent.wait",
"browser.request",
"chat.history",
"chat.abort",
"chat.send"
];
function listGatewayMethods() {
const channelMethods = listChannelPlugins().flatMap((plugin) => plugin.gatewayMethods ?? []);
return Array.from(new Set([...BASE_METHODS, ...channelMethods]));
}
const GATEWAY_EVENTS = [
"connect.challenge",
"agent",
"chat",
"session.message",
"session.tool",
"sessions.changed",
"presence",
"tick",
"talk.mode",
"shutdown",
"health",
"heartbeat",
"cron",
"node.pair.requested",
"node.pair.resolved",
"node.invoke.request",
"device.pair.requested",
"device.pair.resolved",
"voicewake.changed",
"exec.approval.requested",
"exec.approval.resolved",
GATEWAY_EVENT_UPDATE_AVAILABLE
];
//#endregion
//#region src/gateway/control-plane-audit.ts
function normalizePart$1(value, fallback) {
if (typeof value !== "string") return fallback;
const normalized = value.trim();
return normalized.length > 0 ? normalized : fallback;
}
function resolveControlPlaneActor(client) {
return {
actor: normalizePart$1(client?.connect?.client?.id, "unknown-actor"),
deviceId: normalizePart$1(client?.connect?.device?.id, "unknown-device"),
clientIp: normalizePart$1(client?.clientIp, "unknown-ip"),
connId: normalizePart$1(client?.connId, "unknown-conn")
};
}
function formatControlPlaneActor(actor) {
return `actor=${actor.actor} device=${actor.deviceId} ip=${actor.clientIp} conn=${actor.connId}`;
}
function summarizeChangedPaths(paths, maxPaths = 8) {
if (paths.length === 0) return "<none>";
if (paths.length <= maxPaths) return paths.join(",");
return `${paths.slice(0, maxPaths).join(",")},+${paths.length - maxPaths} more`;
}
//#endregion
//#region src/gateway/control-plane-rate-limit.ts
const CONTROL_PLANE_RATE_LIMIT_MAX_REQUESTS = 3;
const CONTROL_PLANE_RATE_LIMIT_WINDOW_MS = 6e4;
const controlPlaneBuckets = /* @__PURE__ */ new Map();
function normalizePart(value, fallback) {
if (typeof value !== "string") return fallback;
const normalized = value.trim();
return normalized.length > 0 ? normalized : fallback;
}
function resolveControlPlaneRateLimitKey(client) {
const deviceId = normalizePart(client?.connect?.device?.id, "unknown-device");
const clientIp = normalizePart(client?.clientIp, "unknown-ip");
if (deviceId === "unknown-device" && clientIp === "unknown-ip") {
const connId = normalizePart(client?.connId, "");
if (connId) return `${deviceId}|${clientIp}|conn=${connId}`;
}
return `${deviceId}|${clientIp}`;
}
function consumeControlPlaneWriteBudget(params) {
const nowMs = params.nowMs ?? Date.now();
const key = resolveControlPlaneRateLimitKey(params.client);
const bucket = controlPlaneBuckets.get(key);
if (!bucket || nowMs - bucket.windowStartMs >= CONTROL_PLANE_RATE_LIMIT_WINDOW_MS) {
controlPlaneBuckets.set(key, {
count: 1,
windowStartMs: nowMs
});
return {
allowed: true,
retryAfterMs: 0,
remaining: CONTROL_PLANE_RATE_LIMIT_MAX_REQUESTS - 1,
key
};
}
if (bucket.count >= CONTROL_PLANE_RATE_LIMIT_MAX_REQUESTS) return {
allowed: false,
retryAfterMs: Math.max(0, bucket.windowStartMs + CONTROL_PLANE_RATE_LIMIT_WINDOW_MS - nowMs),
remaining: 0,
key
};
bucket.count += 1;
return {
allowed: true,
retryAfterMs: 0,
remaining: Math.max(0, CONTROL_PLANE_RATE_LIMIT_MAX_REQUESTS - bucket.count),
key
};
}
//#endregion
//#region src/gateway/role-policy.ts
function parseGatewayRole(roleRaw) {
if (roleRaw === "operator" || roleRaw === "node") return roleRaw;
return null;
}
function roleCanSkipDeviceIdentity(role, sharedAuthOk) {
return role === "operator" && sharedAuthOk;
}
function isRoleAuthorizedForMethod(role, method) {
if (isNodeRoleMethod(method)) return role === "node";
return role === "operator";
}
//#endregion
//#region src/shared/assistant-identity-values.ts
function coerceIdentityValue(value, maxLength) {
if (typeof value !== "string") return;
const trimmed = value.trim();
if (!trimmed) return;
if (trimmed.length <= maxLength) return trimmed;
return trimmed.slice(0, maxLength);
}
//#endregion
//#region src/gateway/assistant-identity.ts
const MAX_ASSISTANT_NAME = 50;
const MAX_ASSISTANT_AVATAR = 200;
const MAX_ASSISTANT_EMOJI = 16;
const DEFAULT_ASSISTANT_IDENTITY = {
agentId: "main",
name: "Assistant",
avatar: "A"
};
function isAvatarUrl(value) {
return isAvatarHttpUrl(value) || isAvatarImageDataUrl(value);
}
function normalizeAvatarValue(value) {
if (!value) return;
const trimmed = value.trim();
if (!trimmed) return;
if (isAvatarUrl(trimmed)) return trimmed;
if (looksLikeAvatarPath(trimmed)) return trimmed;
if (!/\s/.test(trimmed) && trimmed.length <= 4) return trimmed;
}
function normalizeEmojiValue(value) {
if (!value) return;
const trimmed = value.trim();
if (!trimmed) return;
if (trimmed.length > MAX_ASSISTANT_EMOJI) return;
let hasNonAscii = false;
for (let i = 0; i < trimmed.length; i += 1) if (trimmed.charCodeAt(i) > 127) {
hasNonAscii = true;
break;
}
if (!hasNonAscii) return;
if (isAvatarUrl(trimmed) || looksLikeAvatarPath(trimmed)) return;
return trimmed;
}
function resolveAssistantIdentity(params) {
const agentId = normalizeAgentId(params.agentId ?? resolveDefaultAgentId(params.cfg));
const workspaceDir = params.workspaceDir ?? resolveAgentWorkspaceDir(params.cfg, agentId);
const configAssistant = params.cfg.ui?.assistant;
const agentIdentity = resolveAgentIdentity(params.cfg, agentId);
const fileIdentity = workspaceDir ? loadAgentIdentity(workspaceDir) : null;
return {
agentId,
name: coerceIdentityValue(configAssistant?.name, MAX_ASSISTANT_NAME) ?? coerceIdentityValue(agentIdentity?.name, MAX_ASSISTANT_NAME) ?? coerceIdentityValue(fileIdentity?.name, MAX_ASSISTANT_NAME) ?? DEFAULT_ASSISTANT_IDENTITY.name,
avatar: [
coerceIdentityValue(configAssistant?.avatar, MAX_ASSISTANT_AVATAR),
coerceIdentityValue(agentIdentity?.avatar, MAX_ASSISTANT_AVATAR),
coerceIdentityValue(agentIdentity?.emoji, MAX_ASSISTANT_AVATAR),
coerceIdentityValue(fileIdentity?.avatar, MAX_ASSISTANT_AVATAR),
coerceIdentityValue(fileIdentity?.emoji, MAX_ASSISTANT_AVATAR)
].map((candidate) => normalizeAvatarValue(candidate)).find(Boolean) ?? DEFAULT_ASSISTANT_IDENTITY.avatar,
emoji: [
coerceIdentityValue(agentIdentity?.emoji, MAX_ASSISTANT_EMOJI),
coerceIdentityValue(fileIdentity?.emoji, MAX_ASSISTANT_EMOJI),
coerceIdentityValue(agentIdentity?.avatar, MAX_ASSISTANT_EMOJI),
coerceIdentityValue(fileIdentity?.avatar, MAX_ASSISTANT_EMOJI)
].map((candidate) => normalizeEmojiValue(candidate)).find(Boolean)
};
}
//#endregion
//#region src/gateway/session-reset-service.ts
const ACP_RUNTIME_CLEANUP_TIMEOUT_MS = 15e3;
let cachedChannelRuntime$1;
function getChannelRuntime$1() {
cachedChannelRuntime$1 ??= createPluginRuntime().channel;
return cachedChannelRuntime$1;
}
function stripRuntimeModelState(entry) {
if (!entry) return entry;
return {
...entry,
model: void 0,
modelProvider: void 0,
contextTokens: void 0,
systemPromptReport: void 0
};
}
function archiveSessionTranscriptsForSession(params) {
if (!params.sessionId) return [];
return archiveSessionTranscripts({
sessionId: params.sessionId,
storePath: params.storePath,
sessionFile: params.sessionFile,
agentId: params.agentId,
reason: params.reason
});
}
async function emitSessionUnboundLifecycleEvent(params) {
const targetKind = isSubagentSessionKey(params.targetSessionKey) ? "subagent" : "acp";
getChannelRuntime$1().discord.threadBindings.unbindBySessionKey({
targetSessionKey: params.targetSessionKey,
targetKind,
reason: params.reason,
sendFarewell: true
});
if (params.emitHooks === false) return;
const hookRunner = getGlobalHookRunner();
if (!hookRunner?.hasHooks("subagent_ended")) return;
await hookRunner.runSubagentEnded({
targetSessionKey: params.targetSessionKey,
targetKind,
reason: params.reason,
sendFarewell: true,
outcome: params.reason === "session-reset" ? "reset" : "deleted"
}, { childSessionKey: params.targetSessionKey });
}
async function ensureSessionRuntimeCleanup(params) {
const closeTrackedBrowserTabs = async () => {
return await closeTrackedBrowserTabsForSessions({
sessionKeys: [...new Set([
params.key,
params.target.canonicalKey,
...params.target.storeKeys,
params.sessionId ?? ""
])],
onWarn: (message) => logVerbose(message)
});
};
const queueKeys = new Set(params.target.storeKeys);
queueKeys.add(params.target.canonicalKey);
if (params.sessionId) queueKeys.add(params.sessionId);
clearSessionQueues([...queueKeys]);
stopSubagentsForRequester({
cfg: params.cfg,
requesterSessionKey: params.target.canonicalKey
});
if (!params.sessionId) {
clearBootstrapSnapshot(params.target.canonicalKey);
await closeTrackedBrowserTabs();
return;
}
abortEmbeddedPiRun(params.sessionId);
const ended = await waitForEmbeddedPiRunEnd(params.sessionId, 15e3);
clearBootstrapSnapshot(params.target.canonicalKey);
if (ended) {
await closeTrackedBrowserTabs();
return;
}
return errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.key} is still active; try again in a moment.`);
}
async function runAcpCleanupStep(params) {
let timer;
const timeoutPromise = new Promise((resolve) => {
timer = setTimeout(() => resolve({ status: "timeout" }), ACP_RUNTIME_CLEANUP_TIMEOUT_MS);
});
const opPromise = params.op().then(() => ({ status: "ok" })).catch((error) => ({
status: "error",
error
}));
const outcome = await Promise.race([opPromise, timeoutPromise]);
if (timer) clearTimeout(timer);
return outcome;
}
async function closeAcpRuntimeForSession(params) {
if (!params.entry?.acp) return;
const acpManager = getAcpSessionManager();
const cancelOutcome = await runAcpCleanupStep({ op: async () => {
await acpManager.cancelSession({
cfg: params.cfg,
sessionKey: params.sessionKey,
reason: params.reason
});
} });
if (cancelOutcome.status === "timeout") return errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.sessionKey} is still active; try again in a moment.`);
if (cancelOutcome.status === "error") logVerbose(`sessions.${params.reason}: ACP cancel failed for ${params.sessionKey}: ${String(cancelOutcome.error)}`);
const closeOutcome = await runAcpCleanupStep({ op: async () => {
await acpManager.closeSession({
cfg: params.cfg,
sessionKey: params.sessionKey,
reason: params.reason,
requireAcpSession: false,
allowBackendUnavailable: true
});
} });
if (closeOutcome.status === "timeout") return errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.sessionKey} is still active; try again in a moment.`);
if (closeOutcome.status === "error") logVerbose(`sessions.${params.reason}: ACP runtime close failed for ${params.sessionKey}: ${String(closeOutcome.error)}`);
}
async function cleanupSessionBeforeMutation(params) {
const cleanupError = await ensureSessionRuntimeCleanup({
cfg: params.cfg,
key: params.key,
target: params.target,
sessionId: params.entry?.sessionId
});
if (cleanupError) return cleanupError;
return await closeAcpRuntimeForSession({
cfg: params.cfg,
sessionKey: params.legacyKey ?? params.canonicalKey ?? params.target.canonicalKey ?? params.key,
entry: params.entry,
reason: params.reason
});
}
async function performGatewaySessionReset(params) {
const { cfg, target, storePath } = (() => {
const cfg = loadConfig();
const target = resolveGatewaySessionStoreTarget({
cfg,
key: params.key
});
return {
cfg,
target,
storePath: target.storePath
};
})();
const { entry, legacyKey, canonicalKey } = loadSessionEntry(params.key);
const hadExistingEntry = Boolean(entry);
await triggerInternalHook(createInternalHookEvent("command", params.reason, target.canonicalKey ?? params.key, {
sessionEntry: entry,
previousSessionEntry: entry,
commandSource: params.commandSource,
cfg
}));
const mutationCleanupError = await cleanupSessionBeforeMutation({
cfg,
key: params.key,
target,
entry,
legacyKey,
canonicalKey,
reason: "session-reset"
});
if (mutationCleanupError) return {
ok: false,
error: mutationCleanupError
};
let oldSessionId;
let oldSessionFile;
const next = await updateSessionStore(storePath, (store) => {
const { primaryKey } = migrateAndPruneGatewaySessionStoreKey({
cfg,
key: params.key,
store
});
const currentEntry = store[primaryKey];
const resetEntry = stripRuntimeModelState(currentEntry);
const resolvedModel = resolveSessionModelRef(cfg, resetEntry, normalizeAgentId(parseAgentSessionKey(primaryKey)?.agentId ?? resolveDefaultAgentId(cfg)));
oldSessionId = currentEntry?.sessionId;
oldSessionFile = currentEntry?.sessionFile;
const now = Date.now();
const nextEntry = {
sessionId: randomUUID(),
updatedAt: now,
systemSent: false,
abortedLastRun: false,
thinkingLevel: currentEntry?.thinkingLevel,
fastMode: currentEntry?.fastMode,
verboseLevel: currentEntry?.verboseLevel,
reasoningLevel: currentEntry?.reasoningLevel,
responseUsage: currentEntry?.responseUsage,
model: resolvedModel.model,
modelProvider: resolvedModel.provider,
contextTokens: resetEntry?.contextTokens,
sendPolicy: currentEntry?.sendPolicy,
label: currentEntry?.label,
origin: snapshotSessionOrigin(currentEntry),
lastChannel: currentEntry?.lastChannel,
lastTo: currentEntry?.lastTo,
lastAccountId: currentEntry?.lastAccountId,
lastThreadId: currentEntry?.lastThreadId,
skillsSnapshot: currentEntry?.skillsSnapshot,
inputTokens: 0,
outputTokens: 0,
totalTokens: 0,
totalTokensFresh: true
};
store[primaryKey] = nextEntry;
return nextEntry;
});
archiveSessionTranscriptsForSession({
sessionId: oldSessionId,
storePath,
sessionFile: oldSessionFile,
agentId: target.agentId,
reason: "reset"
});
if (hadExistingEntry) await emitSessionUnboundLifecycleEvent({
targetSessionKey: target.canonicalKey ?? params.key,
reason: "session-reset"
});
return {
ok: true,
key: target.canonicalKey,
entry: next
};
}
//#endregion
//#region src/gateway/session-subagent-reactivation.ts
function reactivateCompletedSubagentSession(params) {
const runId = params.runId?.trim();
if (!runId) return false;
const existing = getSubagentRunByChildSessionKey(params.sessionKey);
if (!existing || typeof existing.endedAt !== "number") return false;
return replaceSubagentRunAfterSteer({
previousRunId: existing.runId,
nextRunId: runId,
fallback: existing,
runTimeoutSeconds: existing.runTimeoutSeconds ?? 0
});
}
//#endregion
//#region src/gateway/server-methods/agent-job.ts
const AGENT_RUN_CACHE_TTL_MS = 10 * 6e4;
/**
* Embedded runs can emit transient lifecycle `error` events while auth/model
* failover is still in progress. Give errors a short grace window so a
* subsequent `start` event can cancel premature terminal snapshots.
*/
const AGENT_RUN_ERROR_RETRY_GRACE_MS = 15e3;
const agentRunCache = /* @__PURE__ */ new Map();
const agentRunStarts = /* @__PURE__ */ new Map();
const pendingAgentRunErrors = /* @__PURE__ */ new Map();
let agentRunListenerStarted = false;
function pruneAgentRunCache(now = Date.now()) {
for (const [runId, entry] of agentRunCache) if (now - entry.ts > AGENT_RUN_CACHE_TTL_MS) agentRunCache.delete(runId);
}
function recordAgentRunSnapshot(entry) {
pruneAgentRunCache(entry.ts);
agentRunCache.set(entry.runId, entry);
}
function clearPendingAgentRunError(runId) {
const pending = pendingAgentRunErrors.get(runId);
if (!pending) return;
clearTimeout(pending.timer);
pendingAgentRunErrors.delete(runId);
}
function schedulePendingAgentRunError(snapshot) {
clearPendingAgentRunError(snapshot.runId);
const dueAt = Date.now() + AGENT_RUN_ERROR_RETRY_GRACE_MS;
const timer = setTimeout(() => {
const pending = pendingAgentRunErrors.get(snapshot.runId);
if (!pending) return;
pendingAgentRunErrors.delete(snapshot.runId);
recordAgentRunSnapshot(pending.snapshot);
}, AGENT_RUN_ERROR_RETRY_GRACE_MS);
timer.unref?.();
pendingAgentRunErrors.set(snapshot.runId, {
snapshot,
dueAt,
timer
});
}
function getPendingAgentRunError(runId) {
const pending = pendingAgentRunErrors.get(runId);
if (!pending) return;
return {
snapshot: pending.snapshot,
dueAt: pending.dueAt
};
}
function createSnapshotFromLifecycleEvent(params) {
const { runId, phase, data } = params;
const startedAt = typeof data?.startedAt === "number" ? data.startedAt : agentRunStarts.get(runId);
const endedAt = typeof data?.endedAt === "number" ? data.endedAt : void 0;
const error = typeof data?.error === "string" ? data.error : void 0;
return {
runId,
status: phase === "error" ? "error" : data?.aborted ? "timeout" : "ok",
startedAt,
endedAt,
error,
ts: Date.now()
};
}
function ensureAgentRunListener() {
if (agentRunListenerStarted) return;
agentRunListenerStarted = true;
onAgentEvent((evt) => {
if (!evt) return;
if (evt.stream !== "lifecycle") return;
const phase = evt.data?.phase;
if (phase === "start") {
const startedAt = typeof evt.data?.startedAt === "number" ? evt.data.startedAt : void 0;
agentRunStarts.set(evt.runId, startedAt ?? Date.now());
clearPendingAgentRunError(evt.runId);
agentRunCache.delete(evt.runId);
return;
}
if (phase !== "end" && phase !== "error") return;
const snapshot = createSnapshotFromLifecycleEvent({
runId: evt.runId,
phase,
data: evt.data
});
agentRunStarts.delete(evt.runId);
if (phase === "error") {
schedulePendingAgentRunError(snapshot);
return;
}
clearPendingAgentRunError(evt.runId);
recordAgentRunSnapshot(snapshot);
});
}
function getCachedAgentRun(runId) {
pruneAgentRunCache();
return agentRunCache.get(runId);
}
async function waitForAgentJob(params) {
const { runId, timeoutMs, signal, ignoreCachedSnapshot = false } = params;
ensureAgentRunListener();
const cached = ignoreCachedSnapshot ? void 0 : getCachedAgentRun(runId);
if (cached) return cached;
if (timeoutMs <= 0 || signal?.aborted) return null;
return await new Promise((resolve) => {
let settled = false;
let pendingErrorTimer;
let onAbort;
const clearPendingErrorTimer = () => {
if (!pendingErrorTimer) return;
clearTimeout(pendingErrorTimer);
pendingErrorTimer = void 0;
};
const finish = (entry) => {
if (settled) return;
settled = true;
clearTimeout(timer);
clearPendingErrorTimer();
unsubscribe();
if (onAbort) signal?.removeEventListener("abort", onAbort);
resolve(entry);
};
const scheduleErrorFinish = (snapshot, delayMs = AGENT_RUN_ERROR_RETRY_GRACE_MS) => {
clearPendingErrorTimer();
const effectiveDelay = Math.max(1, Math.min(Math.floor(delayMs), 2147483647));
pendingErrorTimer = setTimeout(() => {
const latest = ignoreCachedSnapshot ? void 0 : getCachedAgentRun(runId);
if (latest) {
finish(latest);
return;
}
recordAgentRunSnapshot(snapshot);
finish(snapshot);
}, effectiveDelay);
pendingErrorTimer.unref?.();
};
if (!ignoreCachedSnapshot) {
const pending = getPendingAgentRunError(runId);
if (pending) scheduleErrorFinish(pending.snapshot, pending.dueAt - Date.now());
}
const unsubscribe = onAgentEvent((evt) => {
if (!evt || evt.stream !== "lifecycle") return;
if (evt.runId !== runId) return;
const phase = evt.data?.phase;
if (phase === "start") {
clearPendingErrorTimer();
return;
}
if (phase !== "end" && phase !== "error") return;
const latest = ignoreCachedSnapshot ? void 0 : getCachedAgentRun(runId);
if (latest) {
finish(latest);
return;
}
const snapshot = createSnapshotFromLifecycleEvent({
runId: evt.runId,
phase,
data: evt.data
});
if (phase === "error") {
scheduleErrorFinish(snapshot);
return;
}
recordAgentRunSnapshot(snapshot);
finish(snapshot);
});
const timerDelayMs = Math.max(1, Math.min(Math.floor(timeoutMs), 2147483647));
const timer = setTimeout(() => finish(null), timerDelayMs);
onAbort = () => finish(null);
signal?.addEventListener("abort", onAbort, { once: true });
});
}
ensureAgentRunListener();
//#endregion
//#region src/gateway/server-methods/agent-timestamp.ts
/**
* Cron jobs inject "Current time: ..." into their messages.
* Skip injection for those.
*/
const CRON_TIME_PATTERN = /Current time: /;
/**
* Matches a leading `[... YYYY-MM-DD HH:MM ...]` envelope — either from
* channel plugins or from a previous injection. Uses the same YYYY-MM-DD
* HH:MM format as {@link formatZonedTimestamp}, so detection stays in sync
* with the formatting.
*/
const TIMESTAMP_ENVELOPE_PATTERN = /^\[.*\d{4}-\d{2}-\d{2} \d{2}:\d{2}/;
/**
* Injects a compact timestamp prefix into a message if one isn't already
* present. Uses the same `YYYY-MM-DD HH:MM TZ` format as channel envelope
* timestamps ({@link formatZonedTimestamp}), keeping token cost low (~7
* tokens) and format consistent across all agent contexts.
*
* Used by the gateway `agent` and `chat.send` handlers to give TUI, web,
* spawned subagents, `sessions_send`, and heartbeat wake events date/time
* awareness — without modifying the system prompt (which is cached).
*
* Channel messages (Discord, Telegram, etc.) already have timestamps via
* envelope formatting and take a separate code path — they never reach
* these handlers, so there is no double-stamping risk. The detection
* pattern is a safety net for edge cases.
*
* @see https://github.com/moltbot/moltbot/issues/3658
*/
function injectTimestamp(message, opts) {
if (!message.trim()) return message;
if (TIMESTAMP_ENVELOPE_PATTERN.test(message)) return message;
if (CRON_TIME_PATTERN.test(message)) return message;
const now = opts?.now ?? /* @__PURE__ */ new Date();
const timezone = opts?.timezone ?? "UTC";
const formatted = formatZonedTimestamp(now, { timeZone: timezone });
if (!formatted) return message;
return `[${new Intl.DateTimeFormat("en-US", {
timeZone: timezone,
weekday: "short"
}).format(now)} ${formatted}] ${message}`;
}
/**
* Build TimestampInjectionOptions from an OpenClawConfig.
*/
function timestampOptsFromConfig(cfg) {
return { timezone: resolveUserTimezone(cfg.agents?.defaults?.userTimezone) };
}
//#endregion
//#region src/gateway/server-methods/agent-wait-dedupe.ts
const AGENT_WAITERS_BY_RUN_ID = /* @__PURE__ */ new Map();
function parseRunIdFromDedupeKey(key) {
if (key.startsWith("agent:")) return key.slice(6) || null;
if (key.startsWith("chat:")) return key.slice(5) || null;
return null;
}
function asFiniteNumber(value) {
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function removeWaiter(runId, waiter) {
const waiters = AGENT_WAITERS_BY_RUN_ID.get(runId);
if (!waiters) return;
waiters.delete(waiter);
if (waiters.size === 0) AGENT_WAITERS_BY_RUN_ID.delete(runId);
}
function addWaiter(runId, waiter) {
const normalizedRunId = runId.trim();
if (!normalizedRunId) return () => {};
const existing = AGENT_WAITERS_BY_RUN_ID.get(normalizedRunId);
if (existing) {
existing.add(waiter);
return () => removeWaiter(normalizedRunId, waiter);
}
AGENT_WAITERS_BY_RUN_ID.set(normalizedRunId, new Set([waiter]));
return () => removeWaiter(normalizedRunId, waiter);
}
function notifyWaiters(runId) {
const normalizedRunId = runId.trim();
if (!normalizedRunId) return;
const waiters = AGENT_WAITERS_BY_RUN_ID.get(normalizedRunId);
if (!waiters || waiters.size === 0) return;
for (const waiter of waiters) waiter();
}
function readTerminalSnapshotFromDedupeEntry(entry) {
const payload = entry.payload;
const status = typeof payload?.status === "string" ? payload.status : void 0;
if (status === "accepted" || status === "started" || status === "in_flight") return null;
const startedAt = asFiniteNumber(payload?.startedAt);
const endedAt = asFiniteNumber(payload?.endedAt) ?? entry.ts;
const errorMessage = typeof payload?.error === "string" ? payload.error : typeof payload?.summary === "string" ? payload.summary : entry.error?.message;
if (status === "ok" || status === "timeout") return {
status,
startedAt,
endedAt,
error: status === "timeout" ? errorMessage : void 0
};
if (status === "error" || !entry.ok) return {
status: "error",
startedAt,
endedAt,
error: errorMessage
};
return null;
}
function readTerminalSnapshotFromGatewayDedupe(params) {
if (params.ignoreAgentTerminalSnapshot) {
const chatEntry = params.dedupe.get(`chat:${params.runId}`);
if (!chatEntry) return null;
return readTerminalSnapshotFromDedupeEntry(chatEntry);
}
const chatEntry = params.dedupe.get(`chat:${params.runId}`);
const chatSnapshot = chatEntry ? readTerminalSnapshotFromDedupeEntry(chatEntry) : null;
const agentEntry = params.dedupe.get(`agent:${params.runId}`);
const agentSnapshot = agentEntry ? readTerminalSnapshotFromDedupeEntry(agentEntry) : null;
if (agentEntry) {
if (!agentSnapshot) {
if (chatSnapshot && chatEntry && chatEntry.ts > agentEntry.ts) return chatSnapshot;
return null;
}
}
if (agentSnapshot && chatSnapshot && agentEntry && chatEntry) return chatEntry.ts > agentEntry.ts ? chatSnapshot : agentSnapshot;
return agentSnapshot ?? chatSnapshot;
}
async function waitForTerminalGatewayDedupe(params) {
const initial = readTerminalSnapshotFromGatewayDedupe(params);
if (initial) return initial;
if (params.timeoutMs <= 0 || params.signal?.aborted) return null;
return await new Promise((resolve) => {
let settled = false;
let timeoutHandle;
let onAbort;
let removeWaiter;
const finish = (snapshot) => {
if (settled) return;
settled = true;
if (timeoutHandle) clearTimeout(timeoutHandle);
if (onAbort) params.signal?.removeEventListener("abort", onAbort);
removeWaiter?.();
resolve(snapshot);
};
const onWake = () => {
const snapshot = readTerminalSnapshotFromGatewayDedupe(params);
if (snapshot) finish(snapshot);
};
removeWaiter = addWaiter(params.runId, onWake);
onWake();
if (settled) return;
const timeoutDelayMs = Math.max(1, Math.min(Math.floor(params.timeoutMs), 2147483647));
timeoutHandle = setTimeout(() => finish(null), timeoutDelayMs);
timeoutHandle.unref?.();
onAbort = () => finish(null);
params.signal?.addEventListener("abort", onAbort, { once: true });
});
}
function setGatewayDedupeEntry(params) {
params.dedupe.set(params.key, params.entry);
const runId = parseRunIdFromDedupeKey(params.key);
if (!runId) return;
if (!readTerminalSnapshotFromDedupeEntry(params.entry)) return;
notifyWaiters(runId);
}
//#endregion
//#region src/gateway/server-methods/agent.ts
const RESET_COMMAND_RE = /^\/(new|reset)(?:\s+([\s\S]*))?$/i;
function resolveSenderIsOwnerFromClient(client) {
return (Array.isArray(client?.connect?.scopes) ? client.connect.scopes : []).includes(ADMIN_SCOPE$2);
}
function resolveAllowModelOverrideFromClient(client) {
return resolveSenderIsOwnerFromClient(client) || client?.internal?.allowModelOverride === true;
}
function resolveCanResetSessionFromClient(client) {
return resolveSenderIsOwnerFromClient(client);
}
async function runSessionResetFromAgent(params) {
const result = await performGatewaySessionReset({
key: params.key,
reason: params.reason,
commandSource: "gateway:agent"
});
if (!result.ok) return result;
return {
ok: true,
key: result.key,
sessionId: result.entry.sessionId
};
}
function emitSessionsChanged$1(context, payload) {
const connIds = context.getSessionEventSubscriberConnIds();
if (connIds.size === 0) return;
const sessionRow = payload.sessionKey ? loadGatewaySessionRow(payload.sessionKey) : null;
context.broadcastToConnIds("sessions.changed", {
...payload,
ts: Date.now(),
...sessionRow ? {
totalTokens: sessionRow.totalTokens,
totalTokensFresh: sessionRow.totalTokensFresh,
contextTokens: sessionRow.contextTokens,
estimatedCostUsd: sessionRow.estimatedCostUsd,
modelProvider: sessionRow.modelProvider,
model: sessionRow.model,
status: sessionRow.status,
startedAt: sessionRow.startedAt,
endedAt: sessionRow.endedAt,
runtimeMs: sessionRow.runtimeMs
} : {}
}, connIds, { dropIfSlow: true });
}
function dispatchAgentRunFromGateway(params) {
agentCommandFromIngress(params.ingressOpts, defaultRuntime, params.context.deps).then((result) => {
const payload = {
runId: params.runId,
status: "ok",
summary: "completed",
result
};
setGatewayDedupeEntry({
dedupe: params.context.dedupe,
key: `agent:${params.idempotencyKey}`,
entry: {
ts: Date.now(),
ok: true,
payload
}
});
params.respond(true, payload, void 0, { runId: params.runId });
}).catch((err) => {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
const payload = {
runId: params.runId,
status: "error",
summary: String(err)
};
setGatewayDedupeEntry({
dedupe: params.context.dedupe,
key: `agent:${params.idempotencyKey}`,
entry: {
ts: Date.now(),
ok: false,
payload,
error
}
});
params.respond(false, payload, error, {
runId: params.runId,
error: formatForLog(err)
});
});
}
const agentHandlers = {
agent: async ({ params, respond, context, client, isWebchatConnect }) => {
const p = params;
if (!validateAgentParams(p)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent params: ${formatValidationErrors(validateAgentParams.errors)}`));
return;
}
const request = p;
const senderIsOwner = resolveSenderIsOwnerFromClient(client);
const allowModelOverride = resolveAllowModelOverrideFromClient(client);
const canResetSession = resolveCanResetSessionFromClient(client);
if (Boolean(request.provider || request.model) && !allowModelOverride) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "provider/model overrides are not authorized for this caller."));
return;
}
const providerOverride = allowModelOverride ? request.provider : void 0;
const modelOverride = allowModelOverride ? request.model : void 0;
const cfg = loadConfig();
const idem = request.idempotencyKey;
const normalizedSpawned = normalizeSpawnedRunMetadata({
groupId: request.groupId,
groupChannel: request.groupChannel,
groupSpace: request.groupSpace
});
let resolvedGroupId = normalizedSpawned.groupId;
let resolvedGroupChannel = normalizedSpawned.groupChannel;
let resolvedGroupSpace = normalizedSpawned.groupSpace;
let spawnedByValue;
const inputProvenance = normalizeInputProvenance(request.inputProvenance);
const cached = context.dedupe.get(`agent:${idem}`);
if (cached) {
respond(cached.ok, cached.payload, cached.error, { cached: true });
return;
}
const normalizedAttachments = normalizeRpcAttachmentsToChatAttachments(request.attachments);
const requestedBestEffortDeliver = typeof request.bestEffortDeliver === "boolean" ? request.bestEffortDeliver : void 0;
let message = (request.message ?? "").trim();
let images = [];
if (normalizedAttachments.length > 0) try {
const parsed = await parseMessageWithAttachments(message, normalizedAttachments, {
maxBytes: 5e6,
log: context.logGateway
});
message = parsed.message.trim();
images = parsed.images;
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, String(err)));
return;
}
const isKnownGatewayChannel = (value) => isGatewayMessageChannel(value);
const channelHints = [request.channel, request.replyChannel].filter((value) => typeof value === "string").map((value) => value.trim()).filter(Boolean);
for (const rawChannel of channelHints) {
const normalized = normalizeMessageChannel(rawChannel);
if (normalized && normalized !== "last" && !isKnownGatewayChannel(normalized)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent params: unknown channel: ${String(normalized)}`));
return;
}
}
const agentIdRaw = typeof request.agentId === "string" ? request.agentId.trim() : "";
const agentId = agentIdRaw ? normalizeAgentId(agentIdRaw) : void 0;
if (agentId) {
if (!listAgentIds(cfg).includes(agentId)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent params: unknown agent id "${request.agentId}"`));
return;
}
}
const requestedSessionKeyRaw = typeof request.sessionKey === "string" && request.sessionKey.trim() ? request.sessionKey.trim() : void 0;
if (requestedSessionKeyRaw && classifySessionKeyShape(requestedSessionKeyRaw) === "malformed_agent") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent params: malformed session key "${requestedSessionKeyRaw}"`));
return;
}
let requestedSessionKey = requestedSessionKeyRaw ?? resolveExplicitAgentSessionKey({
cfg,
agentId
});
if (agentId && requestedSessionKeyRaw) {
const sessionAgentId = resolveAgentIdFromSessionKey(requestedSessionKeyRaw);
if (sessionAgentId !== agentId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent params: agent "${request.agentId}" does not match session key agent "${sessionAgentId}"`));
return;
}
}
let resolvedSessionId = request.sessionId?.trim() || void 0;
let sessionEntry;
let bestEffortDeliver = requestedBestEffortDeliver ?? false;
let cfgForAgent;
let resolvedSessionKey = requestedSessionKey;
let isNewSession = false;
let skipTimestampInjection = false;
const resetCommandMatch = message.match(RESET_COMMAND_RE);
if (resetCommandMatch && requestedSessionKey) {
if (!canResetSession) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `missing scope: ${ADMIN_SCOPE$2}`));
return;
}
const resetReason = resetCommandMatch[1]?.toLowerCase() === "new" ? "new" : "reset";
const resetResult = await runSessionResetFromAgent({
key: requestedSessionKey,
reason: resetReason
});
if (!resetResult.ok) {
respond(false, void 0, resetResult.error);
return;
}
requestedSessionKey = resetResult.key;
resolvedSessionId = resetResult.sessionId ?? resolvedSessionId;
const postResetMessage = resetCommandMatch[2]?.trim() ?? "";
if (postResetMessage) message = postResetMessage;
else {
message = buildBareSessionResetPrompt(cfg);
skipTimestampInjection = true;
}
}
if (!skipTimestampInjection) message = injectTimestamp(message, timestampOptsFromConfig(cfg));
if (requestedSessionKey) {
const { cfg, storePath, entry, canonicalKey } = loadSessionEntry(requestedSessionKey);
cfgForAgent = cfg;
isNewSession = !entry;
const now = Date.now();
const sessionId = entry?.sessionId ?? randomUUID();
const labelValue = request.label?.trim() || entry?.label;
spawnedByValue = canonicalizeSpawnedByForAgent(cfg, resolveAgentIdFromSessionKey(canonicalKey), entry?.spawnedBy);
let inheritedGroup;
if (spawnedByValue && (!resolvedGroupId || !resolvedGroupChannel || !resolvedGroupSpace)) try {
const parentEntry = loadSessionEntry(spawnedByValue)?.entry;
inheritedGroup = {
groupId: parentEntry?.groupId,
groupChannel: parentEntry?.groupChannel,
groupSpace: parentEntry?.space
};
} catch {
inheritedGroup = void 0;
}
resolvedGroupId = resolvedGroupId || inheritedGroup?.groupId;
resolvedGroupChannel = resolvedGroupChannel || inheritedGroup?.groupChannel;
resolvedGroupSpace = resolvedGroupSpace || inheritedGroup?.groupSpace;
const deliveryFields = normalizeSessionDeliveryFields(entry);
const nextEntryPatch = {
sessionId,
updatedAt: now,
thinkingLevel: entry?.thinkingLevel,
fastMode: entry?.fastMode,
verboseLevel: entry?.verboseLevel,
reasoningLevel: entry?.reasoningLevel,
systemSent: entry?.systemSent,
sendPolicy: entry?.sendPolicy,
skillsSnapshot: entry?.skillsSnapshot,
deliveryContext: deliveryFields.deliveryContext,
lastChannel: deliveryFields.lastChannel ?? entry?.lastChannel,
lastTo: deliveryFields.lastTo ?? entry?.lastTo,
lastAccountId: deliveryFields.lastAccountId ?? entry?.lastAccountId,
modelOverride: entry?.modelOverride,
providerOverride: entry?.providerOverride,
label: labelValue,
spawnedBy: spawnedByValue,
spawnedWorkspaceDir: entry?.spawnedWorkspaceDir,
spawnDepth: entry?.spawnDepth,
channel: entry?.channel ?? request.channel?.trim(),
groupId: resolvedGroupId ?? entry?.groupId,
groupChannel: resolvedGroupChannel ?? entry?.groupChannel,
space: resolvedGroupSpace ?? entry?.space,
cliSessionIds: entry?.cliSessionIds,
claudeCliSessionId: entry?.claudeCliSessionId
};
sessionEntry = mergeSessionEntry(entry, nextEntryPatch);
if (resolveSendPolicy({
cfg,
entry,
sessionKey: canonicalKey,
channel: entry?.channel,
chatType: entry?.chatType
}) === "deny") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "send blocked by session policy"));
return;
}
resolvedSessionId = sessionId;
const canonicalSessionKey = canonicalKey;
resolvedSessionKey = canonicalSessionKey;
const mainSessionKey = resolveAgentMainSessionKey({
cfg,
agentId: resolveAgentIdFromSessionKey(canonicalSessionKey)
});
if (storePath) sessionEntry = await updateSessionStore(storePath, (store) => {
const { primaryKey } = migrateAndPruneGatewaySessionStoreKey({
cfg,
key: requestedSessionKey,
store
});
const merged = mergeSessionEntry(store[primaryKey], nextEntryPatch);
store[primaryKey] = merged;
return merged;
});
if (canonicalSessionKey === mainSessionKey || canonicalSessionKey === "global") {
context.addChatRun(idem, {
sessionKey: canonicalSessionKey,
clientRunId: idem
});
if (requestedBestEffortDeliver === void 0) bestEffortDeliver = true;
}
registerAgentRunContext(idem, { sessionKey: canonicalSessionKey });
}
const runId = idem;
const connId = typeof client?.connId === "string" ? client.connId : void 0;
const wantsToolEvents = hasGatewayClientCap(client?.connect?.caps, GATEWAY_CLIENT_CAPS.TOOL_EVENTS);
if (connId && wantsToolEvents) {
context.registerToolEventRecipient(runId, connId);
for (const [activeRunId, active] of context.chatAbortControllers) if (activeRunId !== runId && active.sessionKey === requestedSessionKey) context.registerToolEventRecipient(activeRunId, connId);
}
const wantsDelivery = request.deliver === true;
const explicitTo = typeof request.replyTo === "string" && request.replyTo.trim() ? request.replyTo.trim() : typeof request.to === "string" && request.to.trim() ? request.to.trim() : void 0;
const explicitThreadId = typeof request.threadId === "string" && request.threadId.trim() ? request.threadId.trim() : void 0;
const turnSourceChannel = typeof request.channel === "string" && request.channel.trim() ? request.channel.trim() : void 0;
const turnSourceTo = typeof request.to === "string" && request.to.trim() ? request.to.trim() : void 0;
const turnSourceAccountId = typeof request.accountId === "string" && request.accountId.trim() ? request.accountId.trim() : void 0;
const deliveryPlan = resolveAgentDeliveryPlan({
sessionEntry,
requestedChannel: request.replyChannel ?? request.channel,
explicitTo,
explicitThreadId,
accountId: request.replyAccountId ?? request.accountId,
wantsDelivery,
turnSourceChannel,
turnSourceTo,
turnSourceAccountId,
turnSourceThreadId: explicitThreadId
});
let resolvedChannel = deliveryPlan.resolvedChannel;
let deliveryTargetMode = deliveryPlan.deliveryTargetMode;
let resolvedAccountId = deliveryPlan.resolvedAccountId;
let resolvedTo = deliveryPlan.resolvedTo;
let effectivePlan = deliveryPlan;
if (wantsDelivery && resolvedChannel === "webchat") {
const cfgResolved = cfgForAgent ?? cfg;
try {
resolvedChannel = (await resolveMessageChannelSelection({ cfg: cfgResolved })).channel;
deliveryTargetMode = deliveryTargetMode ?? "implicit";
effectivePlan = {
...deliveryPlan,
resolvedChannel,
deliveryTargetMode,
resolvedAccountId
};
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, String(err)));
return;
}
}
if (!resolvedTo && isDeliverableMessageChannel(resolvedChannel)) {
const fallback = resolveAgentOutboundTarget({
cfg: cfgForAgent ?? cfg,
plan: effectivePlan,
targetMode: deliveryTargetMode ?? "implicit",
validateExplicitTarget: false
});
if (fallback.resolvedTarget?.ok) resolvedTo = fallback.resolvedTo;
}
if (wantsDelivery && resolvedChannel === "webchat") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "delivery channel is required: pass --channel/--reply-channel or use a main session with a previous channel"));
return;
}
const normalizedTurnSource = normalizeMessageChannel(turnSourceChannel);
const originMessageChannel = (normalizedTurnSource && isGatewayMessageChannel(normalizedTurnSource) ? normalizedTurnSource : void 0) ?? (client?.connect && isWebchatConnect(client.connect) ? "webchat" : resolvedChannel);
const deliver = request.deliver === true && resolvedChannel !== "webchat";
const accepted = {
runId,
status: "accepted",
acceptedAt: Date.now()
};
setGatewayDedupeEntry({
dedupe: context.dedupe,
key: `agent:${idem}`,
entry: {
ts: Date.now(),
ok: true,
payload: accepted
}
});
respond(true, accepted, void 0, { runId });
if (resolvedSessionKey) reactivateCompletedSubagentSession({
sessionKey: resolvedSessionKey,
runId
});
if (requestedSessionKey && resolvedSessionKey && isNewSession) emitSessionsChanged$1(context, {
sessionKey: resolvedSessionKey,
reason: "create"
});
if (resolvedSessionKey) emitSessionsChanged$1(context, {
sessionKey: resolvedSessionKey,
reason: "send"
});
const resolvedThreadId = explicitThreadId ?? deliveryPlan.resolvedThreadId;
dispatchAgentRunFromGateway({
ingressOpts: {
message,
images,
provider: providerOverride,
model: modelOverride,
to: resolvedTo,
sessionId: resolvedSessionId,
sessionKey: resolvedSessionKey,
thinking: request.thinking,
deliver,
deliveryTargetMode,
channel: resolvedChannel,
accountId: resolvedAccountId,
threadId: resolvedThreadId,
runContext: {
messageChannel: originMessageChannel,
accountId: resolvedAccountId,
groupId: resolvedGroupId,
groupChannel: resolvedGroupChannel,
groupSpace: resolvedGroupSpace,
currentThreadTs: resolvedThreadId != null ? String(resolvedThreadId) : void 0
},
groupId: resolvedGroupId,
groupChannel: resolvedGroupChannel,
groupSpace: resolvedGroupSpace,
spawnedBy: spawnedByValue,
timeout: request.timeout?.toString(),
bestEffortDeliver,
messageChannel: originMessageChannel,
runId,
lane: request.lane,
extraSystemPrompt: request.extraSystemPrompt,
internalEvents: request.internalEvents,
inputProvenance,
workspaceDir: resolveIngressWorkspaceOverrideForSpawnedRun({
spawnedBy: spawnedByValue,
workspaceDir: sessionEntry?.spawnedWorkspaceDir
}),
senderIsOwner,
allowModelOverride
},
runId,
idempotencyKey: idem,
respond,
context
});
},
"agent.identity.get": ({ params, respond }) => {
if (!validateAgentIdentityParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent.identity.get params: ${formatValidationErrors(validateAgentIdentityParams.errors)}`));
return;
}
const p = params;
const agentIdRaw = typeof p.agentId === "string" ? p.agentId.trim() : "";
const sessionKeyRaw = typeof p.sessionKey === "string" ? p.sessionKey.trim() : "";
let agentId = agentIdRaw ? normalizeAgentId(agentIdRaw) : void 0;
if (sessionKeyRaw) {
if (classifySessionKeyShape(sessionKeyRaw) === "malformed_agent") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent.identity.get params: malformed session key "${sessionKeyRaw}"`));
return;
}
const resolved = resolveAgentIdFromSessionKey(sessionKeyRaw);
if (agentId && resolved !== agentId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent.identity.get params: agent "${agentIdRaw}" does not match session key agent "${resolved}"`));
return;
}
agentId = resolved;
}
const cfg = loadConfig();
const identity = resolveAssistantIdentity({
cfg,
agentId
});
const avatarValue = resolveAssistantAvatarUrl({
avatar: identity.avatar,
agentId: identity.agentId,
basePath: cfg.gateway?.controlUi?.basePath
}) ?? identity.avatar;
respond(true, {
...identity,
avatar: avatarValue
}, void 0);
},
"agent.wait": async ({ params, respond, context }) => {
if (!validateAgentWaitParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agent.wait params: ${formatValidationErrors(validateAgentWaitParams.errors)}`));
return;
}
const p = params;
const runId = (p.runId ?? "").trim();
const timeoutMs = typeof p.timeoutMs === "number" && Number.isFinite(p.timeoutMs) ? Math.max(0, Math.floor(p.timeoutMs)) : 3e4;
const hasActiveChatRun = context.chatAbortControllers.has(runId);
const cachedGatewaySnapshot = readTerminalSnapshotFromGatewayDedupe({
dedupe: context.dedupe,
runId,
ignoreAgentTerminalSnapshot: hasActiveChatRun
});
if (cachedGatewaySnapshot) {
respond(true, {
runId,
status: cachedGatewaySnapshot.status,
startedAt: cachedGatewaySnapshot.startedAt,
endedAt: cachedGatewaySnapshot.endedAt,
error: cachedGatewaySnapshot.error
});
return;
}
const lifecycleAbortController = new AbortController();
const dedupeAbortController = new AbortController();
const lifecyclePromise = waitForAgentJob({
runId,
timeoutMs,
signal: lifecycleAbortController.signal,
ignoreCachedSnapshot: hasActiveChatRun
});
const dedupePromise = waitForTerminalGatewayDedupe({
dedupe: context.dedupe,
runId,
timeoutMs,
signal: dedupeAbortController.signal,
ignoreAgentTerminalSnapshot: hasActiveChatRun
});
const first = await Promise.race([lifecyclePromise.then((snapshot) => ({
source: "lifecycle",
snapshot
})), dedupePromise.then((snapshot) => ({
source: "dedupe",
snapshot
}))]);
let snapshot = first.snapshot;
if (snapshot) if (first.source === "lifecycle") dedupeAbortController.abort();
else lifecycleAbortController.abort();
else {
snapshot = first.source === "lifecycle" ? await dedupePromise : await lifecyclePromise;
lifecycleAbortController.abort();
dedupeAbortController.abort();
}
if (!snapshot) {
respond(true, {
runId,
status: "timeout"
});
return;
}
respond(true, {
runId,
status: snapshot.status,
startedAt: snapshot.startedAt,
endedAt: snapshot.endedAt,
error: snapshot.error
});
}
};
//#endregion
//#region src/gateway/server-methods/agents.ts
const BOOTSTRAP_FILE_NAMES = [
DEFAULT_AGENTS_FILENAME,
DEFAULT_SOUL_FILENAME,
DEFAULT_TOOLS_FILENAME,
DEFAULT_IDENTITY_FILENAME,
DEFAULT_USER_FILENAME,
DEFAULT_HEARTBEAT_FILENAME,
DEFAULT_BOOTSTRAP_FILENAME
];
const BOOTSTRAP_FILE_NAMES_POST_ONBOARDING = BOOTSTRAP_FILE_NAMES.filter((name) => name !== DEFAULT_BOOTSTRAP_FILENAME);
const agentsHandlerDeps = {
isWorkspaceSetupCompleted,
readLocalFileSafely,
resolveAgentWorkspaceFilePath,
writeFileWithinRoot
};
const MEMORY_FILE_NAMES = [DEFAULT_MEMORY_FILENAME, DEFAULT_MEMORY_ALT_FILENAME];
const ALLOWED_FILE_NAMES = new Set([...BOOTSTRAP_FILE_NAMES, ...MEMORY_FILE_NAMES]);
function resolveAgentWorkspaceFileOrRespondError(params, respond) {
const cfg = loadConfig();
const rawAgentId = params.agentId;
const agentId = resolveAgentIdOrError(typeof rawAgentId === "string" || typeof rawAgentId === "number" ? String(rawAgentId) : "", cfg);
if (!agentId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown agent id"));
return null;
}
const rawName = params.name;
const name = (typeof rawName === "string" || typeof rawName === "number" ? String(rawName) : "").trim();
if (!ALLOWED_FILE_NAMES.has(name)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unsupported file "${name}"`));
return null;
}
return {
cfg,
agentId,
workspaceDir: resolveAgentWorkspaceDir(cfg, agentId),
name
};
}
function resolveNotFoundWorkspaceFilePathResult(params) {
if (!isNotFoundPathError(params.error)) return;
if (params.allowMissing) return {
kind: "missing",
requestPath: params.requestPath,
ioPath: params.ioPath,
workspaceReal: params.workspaceReal
};
return {
kind: "invalid",
requestPath: params.requestPath,
reason: "file not found"
};
}
function resolveWorkspaceFilePathResultOrThrow(params) {
const notFoundResult = resolveNotFoundWorkspaceFilePathResult(params);
if (notFoundResult) return notFoundResult;
throw params.error;
}
async function resolveWorkspaceRealPath(workspaceDir) {
try {
return await fs$1.realpath(workspaceDir);
} catch {
return path.resolve(workspaceDir);
}
}
async function resolveAgentWorkspaceFilePath(params) {
const requestPath = path.join(params.workspaceDir, params.name);
const workspaceReal = await resolveWorkspaceRealPath(params.workspaceDir);
const candidatePath = path.resolve(workspaceReal, params.name);
try {
await assertNoPathAliasEscape({
absolutePath: candidatePath,
rootPath: workspaceReal,
boundaryLabel: "workspace root"
});
} catch (error) {
return {
kind: "invalid",
requestPath,
reason: error instanceof Error ? error.message : "path escapes workspace root"
};
}
const notFoundContext = {
allowMissing: params.allowMissing,
requestPath,
workspaceReal
};
let candidateLstat;
try {
candidateLstat = await fs$1.lstat(candidatePath);
} catch (err) {
return resolveWorkspaceFilePathResultOrThrow({
error: err,
...notFoundContext,
ioPath: candidatePath
});
}
if (candidateLstat.isSymbolicLink()) {
let targetReal;
try {
targetReal = await fs$1.realpath(candidatePath);
} catch (err) {
return resolveWorkspaceFilePathResultOrThrow({
error: err,
...notFoundContext,
ioPath: candidatePath
});
}
let targetStat;
try {
targetStat = await fs$1.stat(targetReal);
} catch (err) {
return resolveWorkspaceFilePathResultOrThrow({
error: err,
...notFoundContext,
ioPath: targetReal
});
}
if (!targetStat.isFile()) return {
kind: "invalid",
requestPath,
reason: "path is not a regular file"
};
if (targetStat.nlink > 1) return {
kind: "invalid",
requestPath,
reason: "hardlinked file path not allowed"
};
return {
kind: "ready",
requestPath,
ioPath: targetReal,
workspaceReal
};
}
if (!candidateLstat.isFile()) return {
kind: "invalid",
requestPath,
reason: "path is not a regular file"
};
if (candidateLstat.nlink > 1) return {
kind: "invalid",
requestPath,
reason: "hardlinked file path not allowed"
};
return {
kind: "ready",
requestPath,
ioPath: await fs$1.realpath(candidatePath).catch(() => candidatePath),
workspaceReal
};
}
async function statFileSafely(filePath) {
try {
const [stat, lstat] = await Promise.all([fs$1.stat(filePath), fs$1.lstat(filePath)]);
if (lstat.isSymbolicLink() || !stat.isFile()) return null;
if (stat.nlink > 1) return null;
if (!sameFileIdentity(stat, lstat)) return null;
return {
size: stat.size,
updatedAtMs: Math.floor(stat.mtimeMs)
};
} catch {
return null;
}
}
async function listAgentFiles(workspaceDir, options) {
const files = [];
const bootstrapFileNames = options?.hideBootstrap ? BOOTSTRAP_FILE_NAMES_POST_ONBOARDING : BOOTSTRAP_FILE_NAMES;
for (const name of bootstrapFileNames) {
const resolved = await resolveAgentWorkspaceFilePath({
workspaceDir,
name,
allowMissing: true
});
const filePath = resolved.requestPath;
const meta = resolved.kind === "ready" ? await statFileSafely(resolved.ioPath) : resolved.kind === "missing" ? null : null;
if (meta) files.push({
name,
path: filePath,
missing: false,
size: meta.size,
updatedAtMs: meta.updatedAtMs
});
else files.push({
name,
path: filePath,
missing: true
});
}
const primaryResolved = await resolveAgentWorkspaceFilePath({
workspaceDir,
name: DEFAULT_MEMORY_FILENAME,
allowMissing: true
});
const primaryMeta = primaryResolved.kind === "ready" ? await statFileSafely(primaryResolved.ioPath) : null;
if (primaryMeta) files.push({
name: DEFAULT_MEMORY_FILENAME,
path: primaryResolved.requestPath,
missing: false,
size: primaryMeta.size,
updatedAtMs: primaryMeta.updatedAtMs
});
else {
const altMemoryResolved = await resolveAgentWorkspaceFilePath({
workspaceDir,
name: DEFAULT_MEMORY_ALT_FILENAME,
allowMissing: true
});
const altMeta = altMemoryResolved.kind === "ready" ? await statFileSafely(altMemoryResolved.ioPath) : null;
if (altMeta) files.push({
name: DEFAULT_MEMORY_ALT_FILENAME,
path: altMemoryResolved.requestPath,
missing: false,
size: altMeta.size,
updatedAtMs: altMeta.updatedAtMs
});
else files.push({
name: DEFAULT_MEMORY_FILENAME,
path: primaryResolved.requestPath,
missing: true
});
}
return files;
}
function resolveAgentIdOrError(agentIdRaw, cfg) {
const agentId = normalizeAgentId(agentIdRaw);
if (!new Set(listAgentIds(cfg)).has(agentId)) return null;
return agentId;
}
function sanitizeIdentityLine(value) {
return value.replace(/\s+/g, " ").trim();
}
function resolveOptionalStringParam(value) {
return typeof value === "string" && value.trim() ? value.trim() : void 0;
}
function respondInvalidMethodParams(respond, method, errors) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid ${method} params: ${formatValidationErrors(errors)}`));
}
function isConfiguredAgent(cfg, agentId) {
return findAgentEntryIndex(listAgentEntries(cfg), agentId) >= 0;
}
function respondAgentNotFound(respond, agentId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `agent "${agentId}" not found`));
}
async function moveToTrashBestEffort(pathname) {
if (!pathname) return;
try {
await fs$1.access(pathname);
} catch {
return;
}
try {
await movePathToTrash(pathname);
} catch {}
}
function respondWorkspaceFileInvalid(respond, name, reason) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unsafe workspace file "${name}" (${reason})`));
}
async function resolveWorkspaceFilePathOrRespond(params) {
const resolvedPath = await agentsHandlerDeps.resolveAgentWorkspaceFilePath({
workspaceDir: params.workspaceDir,
name: params.name,
allowMissing: true
});
if (resolvedPath.kind === "invalid") {
respondWorkspaceFileInvalid(params.respond, params.name, resolvedPath.reason);
return;
}
return resolvedPath;
}
function respondWorkspaceFileUnsafe(respond, name) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unsafe workspace file "${name}"`));
}
function respondWorkspaceFileMissing(params) {
params.respond(true, {
agentId: params.agentId,
workspace: params.workspaceDir,
file: {
name: params.name,
path: params.filePath,
missing: true
}
}, void 0);
}
const agentsHandlers = {
"agents.list": ({ params, respond }) => {
if (!validateAgentsListParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agents.list params: ${formatValidationErrors(validateAgentsListParams.errors)}`));
return;
}
respond(true, listAgentsForGateway(loadConfig()), void 0);
},
"agents.create": async ({ params, respond }) => {
if (!validateAgentsCreateParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agents.create params: ${formatValidationErrors(validateAgentsCreateParams.errors)}`));
return;
}
const cfg = loadConfig();
const rawName = String(params.name ?? "").trim();
const agentId = normalizeAgentId(rawName);
if (agentId === "main") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `"${DEFAULT_AGENT_ID}" is reserved`));
return;
}
if (findAgentEntryIndex(listAgentEntries(cfg), agentId) >= 0) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `agent "${agentId}" already exists`));
return;
}
const workspaceDir = resolveUserPath(String(params.workspace ?? "").trim());
let nextConfig = applyAgentConfig(cfg, {
agentId,
name: rawName,
workspace: workspaceDir
});
const agentDir = resolveAgentDir(nextConfig, agentId);
nextConfig = applyAgentConfig(nextConfig, {
agentId,
agentDir
});
await ensureAgentWorkspace({
dir: workspaceDir,
ensureBootstrapFiles: !Boolean(nextConfig.agents?.defaults?.skipBootstrap)
});
await fs$1.mkdir(resolveSessionTranscriptsDirForAgent(agentId), { recursive: true });
await writeConfigFile(nextConfig);
const safeName = sanitizeIdentityLine(rawName);
const emoji = resolveOptionalStringParam(params.emoji);
const avatar = resolveOptionalStringParam(params.avatar);
const identityPath = path.join(workspaceDir, DEFAULT_IDENTITY_FILENAME);
const lines = [
"",
`- Name: ${safeName}`,
...emoji ? [`- Emoji: ${sanitizeIdentityLine(emoji)}`] : [],
...avatar ? [`- Avatar: ${sanitizeIdentityLine(avatar)}`] : [],
""
];
await fs$1.appendFile(identityPath, lines.join("\n"), "utf-8");
respond(true, {
ok: true,
agentId,
name: rawName,
workspace: workspaceDir
}, void 0);
},
"agents.update": async ({ params, respond }) => {
if (!validateAgentsUpdateParams(params)) {
respondInvalidMethodParams(respond, "agents.update", validateAgentsUpdateParams.errors);
return;
}
const cfg = loadConfig();
const agentId = normalizeAgentId(String(params.agentId ?? ""));
if (!isConfiguredAgent(cfg, agentId)) {
respondAgentNotFound(respond, agentId);
return;
}
const workspaceDir = typeof params.workspace === "string" && params.workspace.trim() ? resolveUserPath(params.workspace.trim()) : void 0;
const model = resolveOptionalStringParam(params.model);
const avatar = resolveOptionalStringParam(params.avatar);
const nextConfig = applyAgentConfig(cfg, {
agentId,
...typeof params.name === "string" && params.name.trim() ? { name: params.name.trim() } : {},
...workspaceDir ? { workspace: workspaceDir } : {},
...model ? { model } : {}
});
await writeConfigFile(nextConfig);
if (workspaceDir) await ensureAgentWorkspace({
dir: workspaceDir,
ensureBootstrapFiles: !Boolean(nextConfig.agents?.defaults?.skipBootstrap)
});
if (avatar) {
const workspace = workspaceDir ?? resolveAgentWorkspaceDir(nextConfig, agentId);
await fs$1.mkdir(workspace, { recursive: true });
const identityPath = path.join(workspace, DEFAULT_IDENTITY_FILENAME);
await fs$1.appendFile(identityPath, `\n- Avatar: ${sanitizeIdentityLine(avatar)}\n`, "utf-8");
}
respond(true, {
ok: true,
agentId
}, void 0);
},
"agents.delete": async ({ params, respond }) => {
if (!validateAgentsDeleteParams(params)) {
respondInvalidMethodParams(respond, "agents.delete", validateAgentsDeleteParams.errors);
return;
}
const cfg = loadConfig();
const agentId = normalizeAgentId(String(params.agentId ?? ""));
if (agentId === "main") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `"${DEFAULT_AGENT_ID}" cannot be deleted`));
return;
}
if (!isConfiguredAgent(cfg, agentId)) {
respondAgentNotFound(respond, agentId);
return;
}
const deleteFiles = typeof params.deleteFiles === "boolean" ? params.deleteFiles : true;
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
const agentDir = resolveAgentDir(cfg, agentId);
const sessionsDir = resolveSessionTranscriptsDirForAgent(agentId);
const result = pruneAgentConfig(cfg, agentId);
await writeConfigFile(result.config);
if (deleteFiles) await Promise.all([
moveToTrashBestEffort(workspaceDir),
moveToTrashBestEffort(agentDir),
moveToTrashBestEffort(sessionsDir)
]);
respond(true, {
ok: true,
agentId,
removedBindings: result.removedBindings
}, void 0);
},
"agents.files.list": async ({ params, respond }) => {
if (!validateAgentsFilesListParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid agents.files.list params: ${formatValidationErrors(validateAgentsFilesListParams.errors)}`));
return;
}
const cfg = loadConfig();
const agentId = resolveAgentIdOrError(String(params.agentId ?? ""), cfg);
if (!agentId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown agent id"));
return;
}
const workspaceDir = resolveAgentWorkspaceDir(cfg, agentId);
let hideBootstrap = false;
try {
hideBootstrap = await agentsHandlerDeps.isWorkspaceSetupCompleted(workspaceDir);
} catch {}
respond(true, {
agentId,
workspace: workspaceDir,
files: await listAgentFiles(workspaceDir, { hideBootstrap })
}, void 0);
},
"agents.files.get": async ({ params, respond }) => {
if (!validateAgentsFilesGetParams(params)) {
respondInvalidMethodParams(respond, "agents.files.get", validateAgentsFilesGetParams.errors);
return;
}
const resolved = resolveAgentWorkspaceFileOrRespondError(params, respond);
if (!resolved) return;
const { agentId, workspaceDir, name } = resolved;
const filePath = path.join(workspaceDir, name);
const resolvedPath = await resolveWorkspaceFilePathOrRespond({
respond,
workspaceDir,
name
});
if (!resolvedPath) return;
if (resolvedPath.kind === "missing") {
respondWorkspaceFileMissing({
respond,
agentId,
workspaceDir,
name,
filePath
});
return;
}
let safeRead;
try {
safeRead = await agentsHandlerDeps.readLocalFileSafely({ filePath: resolvedPath.ioPath });
} catch (err) {
if (err instanceof SafeOpenError && err.code === "not-found") {
respondWorkspaceFileMissing({
respond,
agentId,
workspaceDir,
name,
filePath
});
return;
}
respondWorkspaceFileUnsafe(respond, name);
return;
}
respond(true, {
agentId,
workspace: workspaceDir,
file: {
name,
path: filePath,
missing: false,
size: safeRead.stat.size,
updatedAtMs: Math.floor(safeRead.stat.mtimeMs),
content: safeRead.buffer.toString("utf-8")
}
}, void 0);
},
"agents.files.set": async ({ params, respond }) => {
if (!validateAgentsFilesSetParams(params)) {
respondInvalidMethodParams(respond, "agents.files.set", validateAgentsFilesSetParams.errors);
return;
}
const resolved = resolveAgentWorkspaceFileOrRespondError(params, respond);
if (!resolved) return;
const { agentId, workspaceDir, name } = resolved;
await fs$1.mkdir(workspaceDir, { recursive: true });
const filePath = path.join(workspaceDir, name);
const resolvedPath = await resolveWorkspaceFilePathOrRespond({
respond,
workspaceDir,
name
});
if (!resolvedPath) return;
const content = String(params.content ?? "");
const relativeWritePath = path.relative(resolvedPath.workspaceReal, resolvedPath.ioPath);
if (!relativeWritePath || relativeWritePath.startsWith("..") || path.isAbsolute(relativeWritePath)) {
respondWorkspaceFileUnsafe(respond, name);
return;
}
try {
await agentsHandlerDeps.writeFileWithinRoot({
rootDir: resolvedPath.workspaceReal,
relativePath: relativeWritePath,
data: content,
encoding: "utf8"
});
} catch {
respondWorkspaceFileUnsafe(respond, name);
return;
}
const meta = await statFileSafely(resolvedPath.ioPath);
respond(true, {
ok: true,
agentId,
workspace: workspaceDir,
file: {
name,
path: filePath,
missing: false,
size: meta?.size,
updatedAtMs: meta?.updatedAtMs,
content
}
}, void 0);
}
};
//#endregion
//#region src/gateway/server-methods/nodes.helpers.ts
function respondInvalidParams(params) {
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid ${params.method} params: ${formatValidationErrors(params.validator.errors)}`));
}
async function respondUnavailableOnThrow(respond, fn) {
try {
await fn();
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
}
function uniqueSortedStrings(values) {
return [...new Set(values.filter((v) => typeof v === "string"))].map((v) => v.trim()).filter(Boolean).toSorted();
}
function safeParseJson(value) {
if (typeof value !== "string") return;
const trimmed = value.trim();
if (!trimmed) return;
try {
return JSON.parse(trimmed);
} catch {
return { payloadJSON: value };
}
}
function respondUnavailableOnNodeInvokeError(respond, res) {
if (res.ok) return true;
const nodeError = res.error && typeof res.error === "object" ? res.error : null;
const nodeCode = typeof nodeError?.code === "string" ? nodeError.code.trim() : "";
const nodeMessage = typeof nodeError?.message === "string" && nodeError.message.trim().length > 0 ? nodeError.message.trim() : "node invoke failed";
const message = nodeCode ? `${nodeCode}: ${nodeMessage}` : nodeMessage;
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, message, { details: { nodeError: res.error ?? null } }));
return false;
}
//#endregion
//#region src/gateway/server-methods/browser.ts
function isBrowserNode(node) {
const caps = Array.isArray(node.caps) ? node.caps : [];
const commands = Array.isArray(node.commands) ? node.commands : [];
return caps.includes("browser") || commands.includes("browser.proxy");
}
function normalizeNodeKey(value) {
return value.trim().toLowerCase().replace(/[^a-z0-9]+/g, "");
}
function resolveBrowserNode(nodes, query) {
const q = query.trim();
if (!q) return null;
const qNorm = normalizeNodeKey(q);
const matches = nodes.filter((node) => {
if (node.nodeId === q) return true;
if (typeof node.remoteIp === "string" && node.remoteIp === q) return true;
const name = typeof node.displayName === "string" ? node.displayName : "";
if (name && normalizeNodeKey(name) === qNorm) return true;
if (q.length >= 6 && node.nodeId.startsWith(q)) return true;
return false;
});
if (matches.length === 1) return matches[0] ?? null;
if (matches.length === 0) return null;
throw new Error(`ambiguous node: ${q} (matches: ${matches.map((node) => node.displayName || node.remoteIp || node.nodeId).join(", ")})`);
}
function resolveBrowserNodeTarget(params) {
const policy = params.cfg.gateway?.nodes?.browser;
const mode = policy?.mode ?? "auto";
if (mode === "off") return null;
const browserNodes = params.nodes.filter((node) => isBrowserNode(node));
if (browserNodes.length === 0) {
if (policy?.node?.trim()) throw new Error("No connected browser-capable nodes.");
return null;
}
const requested = policy?.node?.trim() || "";
if (requested) {
const resolved = resolveBrowserNode(browserNodes, requested);
if (!resolved) throw new Error(`Configured browser node not connected: ${requested}`);
return resolved;
}
if (mode === "manual") return null;
if (browserNodes.length === 1) return browserNodes[0] ?? null;
return null;
}
async function persistProxyFiles(files) {
return await persistBrowserProxyFiles(files);
}
function applyProxyPaths(result, mapping) {
applyBrowserProxyPaths(result, mapping);
}
const browserHandlers = { "browser.request": async ({ params, respond, context }) => {
const typed = params;
const methodRaw = typeof typed.method === "string" ? typed.method.trim().toUpperCase() : "";
const path = typeof typed.path === "string" ? typed.path.trim() : "";
const query = typed.query && typeof typed.query === "object" ? typed.query : void 0;
const body = typed.body;
const timeoutMs = typeof typed.timeoutMs === "number" && Number.isFinite(typed.timeoutMs) ? Math.max(1, Math.floor(typed.timeoutMs)) : void 0;
if (!methodRaw || !path) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "method and path are required"));
return;
}
if (methodRaw !== "GET" && methodRaw !== "POST" && methodRaw !== "DELETE") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "method must be GET, POST, or DELETE"));
return;
}
if (isPersistentBrowserProfileMutation(methodRaw, path)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "browser.request cannot create or delete persistent browser profiles"));
return;
}
const cfg = loadConfig();
let nodeTarget = null;
try {
nodeTarget = resolveBrowserNodeTarget({
cfg,
nodes: context.nodeRegistry.listConnected()
});
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
return;
}
if (nodeTarget) {
const allowlist = resolveNodeCommandAllowlist(cfg, nodeTarget);
const allowed = isNodeCommandAllowed({
command: "browser.proxy",
declaredCommands: nodeTarget.commands,
allowlist
});
if (!allowed.ok) {
const platform = nodeTarget.platform ?? "unknown";
const hint = `node command not allowed: ${allowed.reason} (platform: ${platform}, command: browser.proxy)`;
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, hint, { details: {
reason: allowed.reason,
command: "browser.proxy"
} }));
return;
}
const proxyParams = {
method: methodRaw,
path,
query,
body,
timeoutMs,
profile: resolveRequestedBrowserProfile({
query,
body
})
};
const res = await context.nodeRegistry.invoke({
nodeId: nodeTarget.nodeId,
command: "browser.proxy",
params: proxyParams,
timeoutMs,
idempotencyKey: crypto.randomUUID()
});
if (!respondUnavailableOnNodeInvokeError(respond, res)) return;
const payload = res.payloadJSON ? safeParseJson(res.payloadJSON) : res.payload;
const proxy = payload && typeof payload === "object" ? payload : null;
if (!proxy || !("result" in proxy)) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "browser proxy failed"));
return;
}
const mapping = await persistProxyFiles(proxy.files);
applyProxyPaths(proxy.result, mapping);
respond(true, proxy.result);
return;
}
if (!await startBrowserControlServiceFromConfig()) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "browser control is disabled"));
return;
}
let dispatcher;
try {
dispatcher = createBrowserRouteDispatcher(createBrowserControlContext());
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
return;
}
const result = await dispatcher.dispatch({
method: methodRaw,
path,
query,
body
});
if (result.status >= 400) {
const message = result.body && typeof result.body === "object" && "error" in result.body ? String(result.body.error) : `browser request failed (${result.status})`;
respond(false, void 0, errorShape(result.status >= 500 ? ErrorCodes.UNAVAILABLE : ErrorCodes.INVALID_REQUEST, message, { details: result.body }));
return;
}
respond(true, result.body);
} };
//#endregion
//#region src/gateway/server-methods/channels.ts
async function logoutChannelAccount(params) {
const resolvedAccountId = params.accountId?.trim() || params.plugin.config.defaultAccountId?.(params.cfg) || params.plugin.config.listAccountIds(params.cfg)[0] || "default";
const account = params.plugin.config.resolveAccount(params.cfg, resolvedAccountId);
await params.context.stopChannel(params.channelId, resolvedAccountId);
const result = await params.plugin.gateway?.logoutAccount?.({
cfg: params.cfg,
accountId: resolvedAccountId,
account,
runtime: defaultRuntime
});
if (!result) throw new Error(`Channel ${params.channelId} does not support logout`);
const cleared = Boolean(result.cleared);
if (typeof result.loggedOut === "boolean" ? result.loggedOut : cleared) params.context.markChannelLoggedOut(params.channelId, true, resolvedAccountId);
return {
channel: params.channelId,
accountId: resolvedAccountId,
...result,
cleared
};
}
const channelsHandlers = {
"channels.status": async ({ params, respond, context }) => {
if (!validateChannelsStatusParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid channels.status params: ${formatValidationErrors(validateChannelsStatusParams.errors)}`));
return;
}
const probe = params.probe === true;
const timeoutMsRaw = params.timeoutMs;
const timeoutMs = typeof timeoutMsRaw === "number" ? Math.max(1e3, timeoutMsRaw) : 1e4;
const cfg = loadConfig();
const runtime = context.getRuntimeSnapshot();
const plugins = listChannelPlugins();
const pluginMap = new Map(plugins.map((plugin) => [plugin.id, plugin]));
const resolveRuntimeSnapshot = (channelId, accountId, defaultAccountId) => {
const accounts = runtime.channelAccounts[channelId];
const defaultRuntime = runtime.channels[channelId];
const raw = accounts?.[accountId] ?? (accountId === defaultAccountId ? defaultRuntime : void 0);
if (!raw) return;
return raw;
};
const isAccountEnabled = (plugin, account) => plugin.config.isEnabled ? plugin.config.isEnabled(account, cfg) : !account || typeof account !== "object" || account.enabled !== false;
const buildChannelAccounts = async (channelId) => {
const plugin = pluginMap.get(channelId);
if (!plugin) return {
accounts: [],
defaultAccountId: DEFAULT_ACCOUNT_ID,
defaultAccount: void 0,
resolvedAccounts: {}
};
const accountIds = plugin.config.listAccountIds(cfg);
const defaultAccountId = resolveChannelDefaultAccountId({
plugin,
cfg,
accountIds
});
const accounts = [];
const resolvedAccounts = {};
for (const accountId of accountIds) {
const account = plugin.config.resolveAccount(cfg, accountId);
const enabled = isAccountEnabled(plugin, account);
resolvedAccounts[accountId] = account;
let probeResult;
let lastProbeAt = null;
if (probe && enabled && plugin.status?.probeAccount) {
let configured = true;
if (plugin.config.isConfigured) configured = await plugin.config.isConfigured(account, cfg);
if (configured) {
probeResult = await plugin.status.probeAccount({
account,
timeoutMs,
cfg
});
lastProbeAt = Date.now();
}
}
let auditResult;
if (probe && enabled && plugin.status?.auditAccount) {
let configured = true;
if (plugin.config.isConfigured) configured = await plugin.config.isConfigured(account, cfg);
if (configured) auditResult = await plugin.status.auditAccount({
account,
timeoutMs,
cfg,
probe: probeResult
});
}
const snapshot = await buildChannelAccountSnapshot({
plugin,
cfg,
accountId,
runtime: resolveRuntimeSnapshot(channelId, accountId, defaultAccountId),
probe: probeResult,
audit: auditResult
});
if (lastProbeAt) snapshot.lastProbeAt = lastProbeAt;
const activity = getChannelActivity({
channel: channelId,
accountId
});
if (snapshot.lastInboundAt == null) snapshot.lastInboundAt = activity.inboundAt;
if (snapshot.lastOutboundAt == null) snapshot.lastOutboundAt = activity.outboundAt;
accounts.push(snapshot);
}
return {
accounts,
defaultAccountId,
defaultAccount: accounts.find((entry) => entry.accountId === defaultAccountId) ?? accounts[0],
resolvedAccounts
};
};
const uiCatalog = buildChannelUiCatalog(plugins);
const payload = {
ts: Date.now(),
channelOrder: uiCatalog.order,
channelLabels: uiCatalog.labels,
channelDetailLabels: uiCatalog.detailLabels,
channelSystemImages: uiCatalog.systemImages,
channelMeta: uiCatalog.entries,
channels: {},
channelAccounts: {},
channelDefaultAccountId: {}
};
const channelsMap = payload.channels;
const accountsMap = payload.channelAccounts;
const defaultAccountIdMap = payload.channelDefaultAccountId;
for (const plugin of plugins) {
const { accounts, defaultAccountId, defaultAccount, resolvedAccounts } = await buildChannelAccounts(plugin.id);
const fallbackAccount = resolvedAccounts[defaultAccountId] ?? plugin.config.resolveAccount(cfg, defaultAccountId);
const summary = plugin.status?.buildChannelSummary ? await plugin.status.buildChannelSummary({
account: fallbackAccount,
cfg,
defaultAccountId,
snapshot: defaultAccount ?? { accountId: defaultAccountId }
}) : { configured: defaultAccount?.configured ?? false };
channelsMap[plugin.id] = summary;
accountsMap[plugin.id] = accounts;
defaultAccountIdMap[plugin.id] = defaultAccountId;
}
respond(true, payload, void 0);
},
"channels.logout": async ({ params, respond, context }) => {
if (!validateChannelsLogoutParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid channels.logout params: ${formatValidationErrors(validateChannelsLogoutParams.errors)}`));
return;
}
const rawChannel = params.channel;
const channelId = typeof rawChannel === "string" ? normalizeChannelId(rawChannel) : null;
if (!channelId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid channels.logout channel"));
return;
}
const plugin = getChannelPlugin(channelId);
if (!plugin?.gateway?.logoutAccount) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `channel ${channelId} does not support logout`));
return;
}
const accountIdRaw = params.accountId;
const accountId = typeof accountIdRaw === "string" ? accountIdRaw.trim() : void 0;
const snapshot = await readConfigFileSnapshot();
if (!snapshot.valid) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "config invalid; fix it before logging out"));
return;
}
try {
respond(true, await logoutChannelAccount({
channelId,
accountId,
cfg: snapshot.config ?? {},
context,
plugin
}), void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
}
};
//#endregion
//#region src/gateway/server-methods/chat-transcript-inject.ts
function appendInjectedAssistantMessageToTranscript(params) {
const now = params.now ?? Date.now();
const messageBody = {
role: "assistant",
content: [{
type: "text",
text: `${params.label ? `[${params.label}]\n\n` : ""}${params.message}`
}],
timestamp: now,
stopReason: "stop",
usage: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
cost: {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
total: 0
}
},
api: "openai-responses",
provider: "openclaw",
model: "gateway-injected",
...params.idempotencyKey ? { idempotencyKey: params.idempotencyKey } : {},
...params.abortMeta ? { openclawAbort: {
aborted: true,
origin: params.abortMeta.origin,
runId: params.abortMeta.runId
} } : {}
};
try {
const messageId = SessionManager.open(params.transcriptPath).appendMessage(messageBody);
emitSessionTranscriptUpdate({
sessionFile: params.transcriptPath,
message: messageBody,
messageId
});
return {
ok: true,
messageId,
message: messageBody
};
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err)
};
}
}
//#endregion
//#region src/gateway/server-methods/chat.ts
const CHAT_HISTORY_TEXT_MAX_CHARS = 12e3;
const CHAT_HISTORY_MAX_SINGLE_MESSAGE_BYTES = 128 * 1024;
const CHAT_HISTORY_OVERSIZED_PLACEHOLDER = "[chat.history omitted: message too large]";
let chatHistoryPlaceholderEmitCount = 0;
const CHANNEL_AGNOSTIC_SESSION_SCOPES = new Set([
"main",
"direct",
"dm",
"group",
"channel",
"cron",
"run",
"subagent",
"acp",
"thread",
"topic"
]);
const CHANNEL_SCOPED_SESSION_SHAPES = new Set([
"direct",
"dm",
"group",
"channel"
]);
function resolveChatSendOriginatingRoute(params) {
if (!(params.deliver === true)) return {
originatingChannel: INTERNAL_MESSAGE_CHANNEL,
explicitDeliverRoute: false
};
const routeChannelCandidate = normalizeMessageChannel(params.entry?.deliveryContext?.channel ?? params.entry?.lastChannel);
const routeToCandidate = params.entry?.deliveryContext?.to ?? params.entry?.lastTo;
const routeAccountIdCandidate = params.entry?.deliveryContext?.accountId ?? params.entry?.lastAccountId ?? void 0;
const routeThreadIdCandidate = params.entry?.deliveryContext?.threadId ?? params.entry?.lastThreadId;
if (params.sessionKey.length > 512) return {
originatingChannel: INTERNAL_MESSAGE_CHANNEL,
explicitDeliverRoute: false
};
const sessionScopeParts = (parseAgentSessionKey(params.sessionKey)?.rest ?? params.sessionKey).split(":", 3).filter(Boolean);
const sessionScopeHead = sessionScopeParts[0];
const sessionChannelHint = normalizeMessageChannel(sessionScopeHead);
const normalizedSessionScopeHead = (sessionScopeHead ?? "").trim().toLowerCase();
const sessionPeerShapeCandidates = [sessionScopeParts[1], sessionScopeParts[2]].map((part) => (part ?? "").trim().toLowerCase()).filter(Boolean);
const isChannelAgnosticSessionScope = CHANNEL_AGNOSTIC_SESSION_SCOPES.has(normalizedSessionScopeHead);
const isChannelScopedSession = sessionPeerShapeCandidates.some((part) => CHANNEL_SCOPED_SESSION_SHAPES.has(part));
const hasLegacyChannelPeerShape = !isChannelScopedSession && typeof sessionScopeParts[1] === "string" && sessionChannelHint === routeChannelCandidate;
const isFromWebchatClient = isWebchatClient(params.client);
const isFromGatewayCliClient = isGatewayCliClient(params.client);
const hasClientMetadata = typeof params.client?.mode === "string" && params.client.mode.trim().length > 0 || typeof params.client?.id === "string" && params.client.id.trim().length > 0;
const configuredMainKey = (params.mainKey ?? "main").trim().toLowerCase();
const canInheritConfiguredMainRoute = normalizedSessionScopeHead.length > 0 && normalizedSessionScopeHead === configuredMainKey && params.hasConnectedClient && (isFromGatewayCliClient || !hasClientMetadata);
if (!(Boolean(!isFromWebchatClient && sessionChannelHint && sessionChannelHint !== "webchat" && (!isChannelAgnosticSessionScope && (isChannelScopedSession || hasLegacyChannelPeerShape) || canInheritConfiguredMainRoute)) && routeChannelCandidate && routeChannelCandidate !== "webchat" && typeof routeToCandidate === "string" && routeToCandidate.trim().length > 0)) return {
originatingChannel: INTERNAL_MESSAGE_CHANNEL,
explicitDeliverRoute: false
};
return {
originatingChannel: routeChannelCandidate,
originatingTo: routeToCandidate,
accountId: routeAccountIdCandidate,
messageThreadId: routeThreadIdCandidate,
explicitDeliverRoute: true
};
}
function stripDisallowedChatControlChars(message) {
let output = "";
for (const char of message) {
const code = char.charCodeAt(0);
if (code === 9 || code === 10 || code === 13 || code >= 32 && code !== 127) output += char;
}
return output;
}
function sanitizeChatSendMessageInput(message) {
const normalized = message.normalize("NFC");
if (normalized.includes("\0")) return {
ok: false,
error: "message must not contain null bytes"
};
return {
ok: true,
message: stripDisallowedChatControlChars(normalized)
};
}
function normalizeOptionalChatSystemReceipt(value) {
if (value == null) return { ok: true };
if (typeof value !== "string") return {
ok: false,
error: "systemProvenanceReceipt must be a string"
};
const sanitized = sanitizeChatSendMessageInput(value);
if (!sanitized.ok) return sanitized;
return {
ok: true,
receipt: sanitized.message.trim() || void 0
};
}
function isAcpBridgeClient(client) {
const info = client?.connect?.client;
return info?.id === GATEWAY_CLIENT_NAMES.CLI && info?.mode === GATEWAY_CLIENT_MODES.CLI && info?.displayName === "ACP" && info?.version === "acp";
}
async function persistChatSendImages(params) {
if (params.images.length === 0 || isAcpBridgeClient(params.client)) return [];
const saved = [];
for (const img of params.images) try {
saved.push(await saveMediaBuffer(Buffer.from(img.data, "base64"), img.mimeType, "inbound"));
} catch (err) {
params.logGateway.warn(`chat.send: failed to persist inbound image (${img.mimeType}): ${formatForLog(err)}`);
}
return saved;
}
function buildChatSendTranscriptMessage(params) {
const mediaFields = resolveChatSendTranscriptMediaFields(params.savedImages);
return {
role: "user",
content: params.message,
timestamp: params.timestamp,
...mediaFields
};
}
function resolveChatSendTranscriptMediaFields(savedImages) {
const mediaPaths = savedImages.map((entry) => entry.path);
if (mediaPaths.length === 0) return {};
const mediaTypes = savedImages.map((entry) => entry.contentType ?? "application/octet-stream");
return {
MediaPath: mediaPaths[0],
MediaPaths: mediaPaths,
MediaType: mediaTypes[0],
MediaTypes: mediaTypes
};
}
function extractTranscriptUserText(content) {
if (typeof content === "string") return content;
if (!Array.isArray(content)) return;
const textBlocks = content.map((block) => block && typeof block === "object" && "text" in block ? block.text : void 0).filter((text) => typeof text === "string");
return textBlocks.length > 0 ? textBlocks.join("") : void 0;
}
async function rewriteChatSendUserTurnMediaPaths(params) {
const mediaFields = resolveChatSendTranscriptMediaFields(params.savedImages);
if (!("MediaPath" in mediaFields)) return;
const target = [...SessionManager.open(params.transcriptPath).getBranch()].toReversed().find((entry) => {
if (entry.type !== "message" || entry.message.role !== "user") return false;
const existingPaths = Array.isArray(entry.message.MediaPaths) ? entry.message.MediaPaths : void 0;
if (typeof entry.message.MediaPath === "string" && entry.message.MediaPath || existingPaths && existingPaths.length > 0) return false;
return extractTranscriptUserText(entry.message.content) === params.message;
});
if (!target || target.type !== "message") return;
const rewrittenMessage = {
...target.message,
...mediaFields
};
await rewriteTranscriptEntriesInSessionFile({
sessionFile: params.transcriptPath,
sessionKey: params.sessionKey,
request: { replacements: [{
entryId: target.id,
message: rewrittenMessage
}] }
});
}
function truncateChatHistoryText(text) {
if (text.length <= CHAT_HISTORY_TEXT_MAX_CHARS) return {
text,
truncated: false
};
return {
text: `${text.slice(0, CHAT_HISTORY_TEXT_MAX_CHARS)}\n...(truncated)...`,
truncated: true
};
}
function sanitizeChatHistoryContentBlock(block) {
if (!block || typeof block !== "object") return {
block,
changed: false
};
const entry = { ...block };
let changed = false;
if (typeof entry.text === "string") {
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
const res = truncateChatHistoryText(stripped.text);
entry.text = res.text;
changed ||= stripped.changed || res.truncated;
}
if (typeof entry.partialJson === "string") {
const res = truncateChatHistoryText(entry.partialJson);
entry.partialJson = res.text;
changed ||= res.truncated;
}
if (typeof entry.arguments === "string") {
const res = truncateChatHistoryText(entry.arguments);
entry.arguments = res.text;
changed ||= res.truncated;
}
if (typeof entry.thinking === "string") {
const res = truncateChatHistoryText(entry.thinking);
entry.thinking = res.text;
changed ||= res.truncated;
}
if ("thinkingSignature" in entry) {
delete entry.thinkingSignature;
changed = true;
}
if ((typeof entry.type === "string" ? entry.type : "") === "image" && typeof entry.data === "string") {
const bytes = Buffer.byteLength(entry.data, "utf8");
delete entry.data;
entry.omitted = true;
entry.bytes = bytes;
changed = true;
}
return {
block: changed ? entry : block,
changed
};
}
/**
* Validate that a value is a finite number, returning undefined otherwise.
*/
function toFiniteNumber(x) {
return typeof x === "number" && Number.isFinite(x) ? x : void 0;
}
/**
* Sanitize usage metadata to ensure only finite numeric fields are included.
* Prevents UI crashes from malformed transcript JSON.
*/
function sanitizeUsage(raw) {
if (!raw || typeof raw !== "object") return;
const u = raw;
const out = {};
for (const k of [
"input",
"output",
"totalTokens",
"inputTokens",
"outputTokens",
"cacheRead",
"cacheWrite",
"cache_read_input_tokens",
"cache_creation_input_tokens"
]) {
const n = toFiniteNumber(u[k]);
if (n !== void 0) out[k] = n;
}
if ("cost" in u && u.cost != null && typeof u.cost === "object") {
const sanitizedCost = sanitizeCost(u.cost);
if (sanitizedCost) out.cost = sanitizedCost;
}
return Object.keys(out).length > 0 ? out : void 0;
}
/**
* Sanitize cost metadata to ensure only finite numeric fields are included.
* Prevents UI crashes from calling .toFixed() on non-numbers.
*/
function sanitizeCost(raw) {
if (!raw || typeof raw !== "object") return;
const total = toFiniteNumber(raw.total);
return total !== void 0 ? { total } : void 0;
}
function sanitizeChatHistoryMessage(message) {
if (!message || typeof message !== "object") return {
message,
changed: false
};
const entry = { ...message };
let changed = false;
if ("details" in entry) {
delete entry.details;
changed = true;
}
if (entry.role !== "assistant") {
if ("usage" in entry) {
delete entry.usage;
changed = true;
}
if ("cost" in entry) {
delete entry.cost;
changed = true;
}
} else {
if ("usage" in entry) {
const sanitized = sanitizeUsage(entry.usage);
if (sanitized) entry.usage = sanitized;
else delete entry.usage;
changed = true;
}
if ("cost" in entry) {
const sanitized = sanitizeCost(entry.cost);
if (sanitized) entry.cost = sanitized;
else delete entry.cost;
changed = true;
}
}
if (typeof entry.content === "string") {
const stripped = stripInlineDirectiveTagsForDisplay(entry.content);
const res = truncateChatHistoryText(stripped.text);
entry.content = res.text;
changed ||= stripped.changed || res.truncated;
} else if (Array.isArray(entry.content)) {
const updated = entry.content.map((block) => sanitizeChatHistoryContentBlock(block));
if (updated.some((item) => item.changed)) {
entry.content = updated.map((item) => item.block);
changed = true;
}
}
if (typeof entry.text === "string") {
const stripped = stripInlineDirectiveTagsForDisplay(entry.text);
const res = truncateChatHistoryText(stripped.text);
entry.text = res.text;
changed ||= stripped.changed || res.truncated;
}
return {
message: changed ? entry : message,
changed
};
}
/**
* Extract the visible text from an assistant history message for silent-token checks.
* Returns `undefined` for non-assistant messages or messages with no extractable text.
* When `entry.text` is present it takes precedence over `entry.content` to avoid
* dropping messages that carry real text alongside a stale `content: "NO_REPLY"`.
*/
function extractAssistantTextForSilentCheck(message) {
if (!message || typeof message !== "object") return;
const entry = message;
if (entry.role !== "assistant") return;
if (typeof entry.text === "string") return entry.text;
if (typeof entry.content === "string") return entry.content;
if (!Array.isArray(entry.content) || entry.content.length === 0) return;
const texts = [];
for (const block of entry.content) {
if (!block || typeof block !== "object") return;
const typed = block;
if (typed.type !== "text" || typeof typed.text !== "string") return;
texts.push(typed.text);
}
return texts.length > 0 ? texts.join("\n") : void 0;
}
function sanitizeChatHistoryMessages(messages) {
if (messages.length === 0) return messages;
let changed = false;
const next = [];
for (const message of messages) {
const res = sanitizeChatHistoryMessage(message);
changed ||= res.changed;
const text = extractAssistantTextForSilentCheck(res.message);
if (text !== void 0 && isSilentReplyText(text, "NO_REPLY")) {
changed = true;
continue;
}
next.push(res.message);
}
return changed ? next : messages;
}
function buildOversizedHistoryPlaceholder(message) {
return {
role: message && typeof message === "object" && typeof message.role === "string" ? message.role : "assistant",
timestamp: message && typeof message === "object" && typeof message.timestamp === "number" ? message.timestamp : Date.now(),
content: [{
type: "text",
text: CHAT_HISTORY_OVERSIZED_PLACEHOLDER
}],
__openclaw: {
truncated: true,
reason: "oversized"
}
};
}
function replaceOversizedChatHistoryMessages(params) {
const { messages, maxSingleMessageBytes } = params;
if (messages.length === 0) return {
messages,
replacedCount: 0
};
let replacedCount = 0;
const next = messages.map((message) => {
if (jsonUtf8Bytes(message) <= maxSingleMessageBytes) return message;
replacedCount += 1;
return buildOversizedHistoryPlaceholder(message);
});
return {
messages: replacedCount > 0 ? next : messages,
replacedCount
};
}
function enforceChatHistoryFinalBudget(params) {
const { messages, maxBytes } = params;
if (messages.length === 0) return {
messages,
placeholderCount: 0
};
if (jsonUtf8Bytes(messages) <= maxBytes) return {
messages,
placeholderCount: 0
};
const last = messages.at(-1);
if (last && jsonUtf8Bytes([last]) <= maxBytes) return {
messages: [last],
placeholderCount: 0
};
const placeholder = buildOversizedHistoryPlaceholder(last);
if (jsonUtf8Bytes([placeholder]) <= maxBytes) return {
messages: [placeholder],
placeholderCount: 1
};
return {
messages: [],
placeholderCount: 0
};
}
function resolveTranscriptPath(params) {
const { sessionId, storePath, sessionFile, agentId } = params;
if (!storePath && !sessionFile) return null;
try {
const sessionsDir = storePath ? path.dirname(storePath) : void 0;
return resolveSessionFilePath(sessionId, sessionFile ? { sessionFile } : void 0, sessionsDir || agentId ? {
sessionsDir,
agentId
} : void 0);
} catch {
return null;
}
}
function ensureTranscriptFile(params) {
if (fs.existsSync(params.transcriptPath)) return { ok: true };
try {
fs.mkdirSync(path.dirname(params.transcriptPath), { recursive: true });
const header = {
type: "session",
version: CURRENT_SESSION_VERSION,
id: params.sessionId,
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
cwd: process.cwd()
};
fs.writeFileSync(params.transcriptPath, `${JSON.stringify(header)}\n`, {
encoding: "utf-8",
mode: 384
});
return { ok: true };
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err)
};
}
}
function transcriptHasIdempotencyKey(transcriptPath, idempotencyKey) {
try {
const lines = fs.readFileSync(transcriptPath, "utf-8").split(/\r?\n/);
for (const line of lines) {
if (!line.trim()) continue;
if (JSON.parse(line)?.message?.idempotencyKey === idempotencyKey) return true;
}
return false;
} catch {
return false;
}
}
function appendAssistantTranscriptMessage(params) {
const transcriptPath = resolveTranscriptPath({
sessionId: params.sessionId,
storePath: params.storePath,
sessionFile: params.sessionFile,
agentId: params.agentId
});
if (!transcriptPath) return {
ok: false,
error: "transcript path not resolved"
};
if (!fs.existsSync(transcriptPath)) {
if (!params.createIfMissing) return {
ok: false,
error: "transcript file not found"
};
const ensured = ensureTranscriptFile({
transcriptPath,
sessionId: params.sessionId
});
if (!ensured.ok) return {
ok: false,
error: ensured.error ?? "failed to create transcript file"
};
}
if (params.idempotencyKey && transcriptHasIdempotencyKey(transcriptPath, params.idempotencyKey)) return { ok: true };
return appendInjectedAssistantMessageToTranscript({
transcriptPath,
message: params.message,
label: params.label,
idempotencyKey: params.idempotencyKey,
abortMeta: params.abortMeta
});
}
function collectSessionAbortPartials(params) {
const out = [];
for (const [runId, active] of params.chatAbortControllers) {
if (!params.runIds.has(runId)) continue;
const text = params.chatRunBuffers.get(runId);
if (!text || !text.trim()) continue;
out.push({
runId,
sessionId: active.sessionId,
text,
abortOrigin: params.abortOrigin
});
}
return out;
}
function persistAbortedPartials(params) {
if (params.snapshots.length === 0) return;
const { storePath, entry } = loadSessionEntry(params.sessionKey);
for (const snapshot of params.snapshots) {
const sessionId = entry?.sessionId ?? snapshot.sessionId ?? snapshot.runId;
const appended = appendAssistantTranscriptMessage({
message: snapshot.text,
sessionId,
storePath,
sessionFile: entry?.sessionFile,
createIfMissing: true,
idempotencyKey: `${snapshot.runId}:assistant`,
abortMeta: {
aborted: true,
origin: snapshot.abortOrigin,
runId: snapshot.runId
}
});
if (!appended.ok) params.context.logGateway.warn(`chat.abort transcript append failed: ${appended.error ?? "unknown error"}`);
}
}
function createChatAbortOps(context) {
return {
chatAbortControllers: context.chatAbortControllers,
chatRunBuffers: context.chatRunBuffers,
chatDeltaSentAt: context.chatDeltaSentAt,
chatDeltaLastBroadcastLen: context.chatDeltaLastBroadcastLen,
chatAbortedRuns: context.chatAbortedRuns,
removeChatRun: context.removeChatRun,
agentRunSeq: context.agentRunSeq,
broadcast: context.broadcast,
nodeSendToSession: context.nodeSendToSession
};
}
function normalizeOptionalText(value) {
return value?.trim() || void 0;
}
function resolveChatAbortRequester(client) {
const scopes = Array.isArray(client?.connect?.scopes) ? client.connect.scopes : [];
return {
connId: normalizeOptionalText(client?.connId),
deviceId: normalizeOptionalText(client?.connect?.device?.id),
isAdmin: scopes.includes(ADMIN_SCOPE$2)
};
}
function canRequesterAbortChatRun(entry, requester) {
if (requester.isAdmin) return true;
const ownerDeviceId = normalizeOptionalText(entry.ownerDeviceId);
const ownerConnId = normalizeOptionalText(entry.ownerConnId);
if (!ownerDeviceId && !ownerConnId) return true;
if (ownerDeviceId && requester.deviceId && ownerDeviceId === requester.deviceId) return true;
if (ownerConnId && requester.connId && ownerConnId === requester.connId) return true;
return false;
}
function resolveAuthorizedRunIdsForSession(params) {
const authorizedRunIds = [];
let matchedSessionRuns = 0;
for (const [runId, active] of params.chatAbortControllers) {
if (active.sessionKey !== params.sessionKey) continue;
matchedSessionRuns += 1;
if (canRequesterAbortChatRun(active, params.requester)) authorizedRunIds.push(runId);
}
return {
matchedSessionRuns,
authorizedRunIds
};
}
function abortChatRunsForSessionKeyWithPartials(params) {
const { matchedSessionRuns, authorizedRunIds } = resolveAuthorizedRunIdsForSession({
chatAbortControllers: params.context.chatAbortControllers,
sessionKey: params.sessionKey,
requester: params.requester
});
if (authorizedRunIds.length === 0) return {
aborted: false,
runIds: [],
unauthorized: matchedSessionRuns > 0
};
const authorizedRunIdSet = new Set(authorizedRunIds);
const snapshots = collectSessionAbortPartials({
chatAbortControllers: params.context.chatAbortControllers,
chatRunBuffers: params.context.chatRunBuffers,
runIds: authorizedRunIdSet,
abortOrigin: params.abortOrigin
});
const runIds = [];
for (const runId of authorizedRunIds) if (abortChatRunById(params.ops, {
runId,
sessionKey: params.sessionKey,
stopReason: params.stopReason
}).aborted) runIds.push(runId);
const res = {
aborted: runIds.length > 0,
runIds,
unauthorized: false
};
if (res.aborted) persistAbortedPartials({
context: params.context,
sessionKey: params.sessionKey,
snapshots
});
return res;
}
function nextChatSeq(context, runId) {
const next = (context.agentRunSeq.get(runId) ?? 0) + 1;
context.agentRunSeq.set(runId, next);
return next;
}
function broadcastChatFinal(params) {
const seq = nextChatSeq({ agentRunSeq: params.context.agentRunSeq }, params.runId);
const strippedEnvelopeMessage = stripEnvelopeFromMessage(params.message);
const payload = {
runId: params.runId,
sessionKey: params.sessionKey,
seq,
state: "final",
message: stripInlineDirectiveTagsFromMessageForDisplay(strippedEnvelopeMessage)
};
params.context.broadcast("chat", payload);
params.context.nodeSendToSession(params.sessionKey, "chat", payload);
params.context.agentRunSeq.delete(params.runId);
}
function isBtwReplyPayload(payload) {
return typeof payload?.btw?.question === "string" && payload.btw.question.trim().length > 0 && typeof payload.text === "string" && payload.text.trim().length > 0;
}
function broadcastSideResult(params) {
const seq = nextChatSeq({ agentRunSeq: params.context.agentRunSeq }, params.payload.runId);
params.context.broadcast("chat.side_result", {
...params.payload,
seq
});
params.context.nodeSendToSession(params.payload.sessionKey, "chat.side_result", {
...params.payload,
seq
});
}
function broadcastChatError(params) {
const seq = nextChatSeq({ agentRunSeq: params.context.agentRunSeq }, params.runId);
const payload = {
runId: params.runId,
sessionKey: params.sessionKey,
seq,
state: "error",
errorMessage: params.errorMessage
};
params.context.broadcast("chat", payload);
params.context.nodeSendToSession(params.sessionKey, "chat", payload);
params.context.agentRunSeq.delete(params.runId);
}
const chatHandlers = {
"chat.history": async ({ params, respond, context }) => {
if (!validateChatHistoryParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid chat.history params: ${formatValidationErrors(validateChatHistoryParams.errors)}`));
return;
}
const { sessionKey, limit } = params;
const { cfg, storePath, entry } = loadSessionEntry(sessionKey);
const sessionId = entry?.sessionId;
const rawMessages = sessionId && storePath ? readSessionMessages(sessionId, storePath, entry?.sessionFile) : [];
const hardMax = 1e3;
const requested = typeof limit === "number" ? limit : 200;
const max = Math.min(hardMax, requested);
const normalized = sanitizeChatHistoryMessages(stripEnvelopeFromMessages(rawMessages.length > max ? rawMessages.slice(-max) : rawMessages));
const maxHistoryBytes = getMaxChatHistoryMessagesBytes();
const replaced = replaceOversizedChatHistoryMessages({
messages: normalized,
maxSingleMessageBytes: Math.min(CHAT_HISTORY_MAX_SINGLE_MESSAGE_BYTES, maxHistoryBytes)
});
const capped = capArrayByJsonBytes(replaced.messages, maxHistoryBytes).items;
const bounded = enforceChatHistoryFinalBudget({
messages: capped,
maxBytes: maxHistoryBytes
});
const placeholderCount = replaced.replacedCount + bounded.placeholderCount;
if (placeholderCount > 0) {
chatHistoryPlaceholderEmitCount += placeholderCount;
context.logGateway.debug(`chat.history omitted oversized payloads placeholders=${placeholderCount} total=${chatHistoryPlaceholderEmitCount}`);
}
let thinkingLevel = entry?.thinkingLevel;
if (!thinkingLevel) {
const { provider, model } = resolveSessionModelRef(cfg, entry, resolveSessionAgentId({
sessionKey,
config: cfg
}));
thinkingLevel = resolveThinkingDefault({
cfg,
provider,
model,
catalog: await context.loadGatewayModelCatalog()
});
}
const verboseLevel = entry?.verboseLevel ?? cfg.agents?.defaults?.verboseDefault;
respond(true, {
sessionKey,
sessionId,
messages: bounded.messages,
thinkingLevel,
fastMode: entry?.fastMode,
verboseLevel
});
},
"chat.abort": ({ params, respond, context, client }) => {
if (!validateChatAbortParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid chat.abort params: ${formatValidationErrors(validateChatAbortParams.errors)}`));
return;
}
const { sessionKey: rawSessionKey, runId } = params;
const ops = createChatAbortOps(context);
const requester = resolveChatAbortRequester(client);
if (!runId) {
const res = abortChatRunsForSessionKeyWithPartials({
context,
ops,
sessionKey: rawSessionKey,
abortOrigin: "rpc",
stopReason: "rpc",
requester
});
if (res.unauthorized) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unauthorized"));
return;
}
respond(true, {
ok: true,
aborted: res.aborted,
runIds: res.runIds
});
return;
}
const active = context.chatAbortControllers.get(runId);
if (!active) {
respond(true, {
ok: true,
aborted: false,
runIds: []
});
return;
}
if (active.sessionKey !== rawSessionKey) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "runId does not match sessionKey"));
return;
}
if (!canRequesterAbortChatRun(active, requester)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unauthorized"));
return;
}
const partialText = context.chatRunBuffers.get(runId);
const res = abortChatRunById(ops, {
runId,
sessionKey: rawSessionKey,
stopReason: "rpc"
});
if (res.aborted && partialText && partialText.trim()) persistAbortedPartials({
context,
sessionKey: rawSessionKey,
snapshots: [{
runId,
sessionId: active.sessionId,
text: partialText,
abortOrigin: "rpc"
}]
});
respond(true, {
ok: true,
aborted: res.aborted,
runIds: res.aborted ? [runId] : []
});
},
"chat.send": async ({ params, respond, context, client }) => {
if (!validateChatSendParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid chat.send params: ${formatValidationErrors(validateChatSendParams.errors)}`));
return;
}
const p = params;
if ((p.systemInputProvenance || p.systemProvenanceReceipt) && !isAcpBridgeClient(client)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "system provenance fields are reserved for the ACP bridge"));
return;
}
const sanitizedMessageResult = sanitizeChatSendMessageInput(p.message);
if (!sanitizedMessageResult.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, sanitizedMessageResult.error));
return;
}
const systemReceiptResult = normalizeOptionalChatSystemReceipt(p.systemProvenanceReceipt);
if (!systemReceiptResult.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, systemReceiptResult.error));
return;
}
const inboundMessage = sanitizedMessageResult.message;
const systemInputProvenance = normalizeInputProvenance(p.systemInputProvenance);
const systemProvenanceReceipt = systemReceiptResult.receipt;
const stopCommand = isChatStopCommandText(inboundMessage);
const normalizedAttachments = normalizeRpcAttachmentsToChatAttachments(p.attachments);
if (!inboundMessage.trim() && normalizedAttachments.length === 0) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "message or attachment required"));
return;
}
let parsedMessage = inboundMessage;
let parsedImages = [];
if (normalizedAttachments.length > 0) try {
const parsed = await parseMessageWithAttachments(inboundMessage, normalizedAttachments, {
maxBytes: 5e6,
log: context.logGateway
});
parsedMessage = parsed.message;
parsedImages = parsed.images;
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, String(err)));
return;
}
const rawSessionKey = p.sessionKey;
const { cfg, entry, canonicalKey: sessionKey } = loadSessionEntry(rawSessionKey);
const timeoutMs = resolveAgentTimeoutMs({
cfg,
overrideMs: p.timeoutMs
});
const now = Date.now();
const clientRunId = p.idempotencyKey;
if (resolveSendPolicy({
cfg,
entry,
sessionKey,
channel: entry?.channel,
chatType: entry?.chatType
}) === "deny") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "send blocked by session policy"));
return;
}
if (stopCommand) {
const res = abortChatRunsForSessionKeyWithPartials({
context,
ops: createChatAbortOps(context),
sessionKey: rawSessionKey,
abortOrigin: "stop-command",
stopReason: "stop",
requester: resolveChatAbortRequester(client)
});
if (res.unauthorized) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unauthorized"));
return;
}
respond(true, {
ok: true,
aborted: res.aborted,
runIds: res.runIds
});
return;
}
const cached = context.dedupe.get(`chat:${clientRunId}`);
if (cached) {
respond(cached.ok, cached.payload, cached.error, { cached: true });
return;
}
if (context.chatAbortControllers.get(clientRunId)) {
respond(true, {
runId: clientRunId,
status: "in_flight"
}, void 0, {
cached: true,
runId: clientRunId
});
return;
}
try {
const abortController = new AbortController();
context.chatAbortControllers.set(clientRunId, {
controller: abortController,
sessionId: entry?.sessionId ?? clientRunId,
sessionKey: rawSessionKey,
startedAtMs: now,
expiresAtMs: resolveChatRunExpiresAtMs({
now,
timeoutMs
}),
ownerConnId: normalizeOptionalText(client?.connId),
ownerDeviceId: normalizeOptionalText(client?.connect?.device?.id)
});
respond(true, {
runId: clientRunId,
status: "started"
}, void 0, { runId: clientRunId });
const persistedImagesPromise = persistChatSendImages({
images: parsedImages,
client,
logGateway: context.logGateway
});
const trimmedMessage = parsedMessage.trim();
const commandBody = Boolean(p.thinking && trimmedMessage && !trimmedMessage.startsWith("/")) ? `/think ${p.thinking} ${parsedMessage}` : parsedMessage;
const messageForAgent = systemProvenanceReceipt ? [systemProvenanceReceipt, parsedMessage].filter(Boolean).join("\n\n") : parsedMessage;
const clientInfo = client?.connect?.client;
const { originatingChannel, originatingTo, accountId, messageThreadId, explicitDeliverRoute } = resolveChatSendOriginatingRoute({
client: clientInfo,
deliver: p.deliver,
entry,
hasConnectedClient: client?.connect !== void 0,
mainKey: cfg.session?.mainKey,
sessionKey
});
const ctx = {
Body: messageForAgent,
BodyForAgent: injectTimestamp(messageForAgent, timestampOptsFromConfig(cfg)),
BodyForCommands: commandBody,
RawBody: parsedMessage,
CommandBody: commandBody,
InputProvenance: systemInputProvenance,
SessionKey: sessionKey,
Provider: INTERNAL_MESSAGE_CHANNEL,
Surface: INTERNAL_MESSAGE_CHANNEL,
OriginatingChannel: originatingChannel,
OriginatingTo: originatingTo,
ExplicitDeliverRoute: explicitDeliverRoute,
AccountId: accountId,
MessageThreadId: messageThreadId,
ChatType: "direct",
CommandAuthorized: true,
MessageSid: clientRunId,
SenderId: clientInfo?.id,
SenderName: clientInfo?.displayName,
SenderUsername: clientInfo?.displayName,
GatewayClientScopes: client?.connect?.scopes
};
const agentId = resolveSessionAgentId({
sessionKey,
config: cfg
});
const { onModelSelected, ...replyPipeline } = createChannelReplyPipeline({
cfg,
agentId,
channel: INTERNAL_MESSAGE_CHANNEL
});
const deliveredReplies = [];
let userTranscriptUpdateEmitted = false;
const emitUserTranscriptUpdate = async () => {
if (userTranscriptUpdateEmitted) return;
const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(sessionKey);
const resolvedSessionId = latestEntry?.sessionId ?? entry?.sessionId;
if (!resolvedSessionId) return;
const transcriptPath = resolveTranscriptPath({
sessionId: resolvedSessionId,
storePath: latestStorePath,
sessionFile: latestEntry?.sessionFile ?? entry?.sessionFile,
agentId
});
if (!transcriptPath) return;
userTranscriptUpdateEmitted = true;
const persistedImages = await persistedImagesPromise;
emitSessionTranscriptUpdate({
sessionFile: transcriptPath,
sessionKey,
message: buildChatSendTranscriptMessage({
message: parsedMessage,
savedImages: persistedImages,
timestamp: now
})
});
};
let transcriptMediaRewriteDone = false;
const rewriteUserTranscriptMedia = async () => {
if (transcriptMediaRewriteDone) return;
const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(sessionKey);
const resolvedSessionId = latestEntry?.sessionId ?? entry?.sessionId;
if (!resolvedSessionId) return;
const transcriptPath = resolveTranscriptPath({
sessionId: resolvedSessionId,
storePath: latestStorePath,
sessionFile: latestEntry?.sessionFile ?? entry?.sessionFile,
agentId
});
if (!transcriptPath) return;
transcriptMediaRewriteDone = true;
await rewriteChatSendUserTurnMediaPaths({
transcriptPath,
sessionKey,
message: parsedMessage,
savedImages: await persistedImagesPromise
});
};
const dispatcher = createReplyDispatcher({
...replyPipeline,
onError: (err) => {
context.logGateway.warn(`webchat dispatch failed: ${formatForLog(err)}`);
},
deliver: async (payload, info) => {
if (info.kind !== "block" && info.kind !== "final") return;
deliveredReplies.push({
payload,
kind: info.kind
});
}
});
let agentRunStarted = false;
dispatchInboundMessage({
ctx,
cfg,
dispatcher,
replyOptions: {
runId: clientRunId,
abortSignal: abortController.signal,
images: parsedImages.length > 0 ? parsedImages : void 0,
onAgentRunStart: (runId) => {
agentRunStarted = true;
emitUserTranscriptUpdate();
const connId = typeof client?.connId === "string" ? client.connId : void 0;
const wantsToolEvents = hasGatewayClientCap(client?.connect?.caps, GATEWAY_CLIENT_CAPS.TOOL_EVENTS);
if (connId && wantsToolEvents) {
context.registerToolEventRecipient(runId, connId);
for (const [activeRunId, active] of context.chatAbortControllers) if (activeRunId !== runId && active.sessionKey === p.sessionKey) context.registerToolEventRecipient(activeRunId, connId);
}
},
onModelSelected
}
}).then(async () => {
await rewriteUserTranscriptMedia();
if (!agentRunStarted) {
await emitUserTranscriptUpdate();
const btwReplies = deliveredReplies.map((entry) => entry.payload).filter(isBtwReplyPayload);
const btwText = btwReplies.map((payload) => payload.text.trim()).filter(Boolean).join("\n\n").trim();
if (btwReplies.length > 0 && btwText) {
broadcastSideResult({
context,
payload: {
kind: "btw",
runId: clientRunId,
sessionKey: rawSessionKey,
question: btwReplies[0].btw.question.trim(),
text: btwText,
isError: btwReplies.some((payload) => payload.isError),
ts: Date.now()
}
});
broadcastChatFinal({
context,
runId: clientRunId,
sessionKey: rawSessionKey
});
} else {
const combinedReply = deliveredReplies.filter((entry) => entry.kind === "final").map((entry) => entry.payload).map((part) => part.text?.trim() ?? "").filter(Boolean).join("\n\n").trim();
let message;
if (combinedReply) {
const { storePath: latestStorePath, entry: latestEntry } = loadSessionEntry(sessionKey);
const appended = appendAssistantTranscriptMessage({
message: combinedReply,
sessionId: latestEntry?.sessionId ?? entry?.sessionId ?? clientRunId,
storePath: latestStorePath,
sessionFile: latestEntry?.sessionFile,
agentId,
createIfMissing: true
});
if (appended.ok) message = appended.message;
else {
context.logGateway.warn(`webchat transcript append failed: ${appended.error ?? "unknown error"}`);
message = {
role: "assistant",
content: [{
type: "text",
text: combinedReply
}],
timestamp: Date.now(),
stopReason: "stop",
usage: {
input: 0,
output: 0,
totalTokens: 0
}
};
}
}
broadcastChatFinal({
context,
runId: clientRunId,
sessionKey: rawSessionKey,
message
});
}
} else emitUserTranscriptUpdate();
setGatewayDedupeEntry({
dedupe: context.dedupe,
key: `chat:${clientRunId}`,
entry: {
ts: Date.now(),
ok: true,
payload: {
runId: clientRunId,
status: "ok"
}
}
});
}).catch((err) => {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
setGatewayDedupeEntry({
dedupe: context.dedupe,
key: `chat:${clientRunId}`,
entry: {
ts: Date.now(),
ok: false,
payload: {
runId: clientRunId,
status: "error",
summary: String(err)
},
error
}
});
broadcastChatError({
context,
runId: clientRunId,
sessionKey: rawSessionKey,
errorMessage: String(err)
});
}).finally(() => {
context.chatAbortControllers.delete(clientRunId);
});
} catch (err) {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
const payload = {
runId: clientRunId,
status: "error",
summary: String(err)
};
setGatewayDedupeEntry({
dedupe: context.dedupe,
key: `chat:${clientRunId}`,
entry: {
ts: Date.now(),
ok: false,
payload,
error
}
});
respond(false, payload, error, {
runId: clientRunId,
error: formatForLog(err)
});
}
},
"chat.inject": async ({ params, respond, context }) => {
if (!validateChatInjectParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid chat.inject params: ${formatValidationErrors(validateChatInjectParams.errors)}`));
return;
}
const p = params;
const rawSessionKey = p.sessionKey;
const { cfg, storePath, entry } = loadSessionEntry(rawSessionKey);
const sessionId = entry?.sessionId;
if (!sessionId || !storePath) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "session not found"));
return;
}
const appended = appendAssistantTranscriptMessage({
message: p.message,
label: p.label,
sessionId,
storePath,
sessionFile: entry?.sessionFile,
agentId: resolveSessionAgentId({
sessionKey: rawSessionKey,
config: cfg
}),
createIfMissing: true
});
if (!appended.ok || !appended.messageId || !appended.message) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, `failed to write transcript: ${appended.error ?? "unknown error"}`));
return;
}
const chatPayload = {
runId: `inject-${appended.messageId}`,
sessionKey: rawSessionKey,
seq: 0,
state: "final",
message: stripInlineDirectiveTagsFromMessageForDisplay(stripEnvelopeFromMessage(appended.message))
};
context.broadcast("chat", chatPayload);
context.nodeSendToSession(rawSessionKey, "chat", chatPayload);
respond(true, {
ok: true,
messageId: appended.messageId
});
}
};
//#endregion
//#region src/config/schema.base.generated.ts
const GENERATED_BASE_CONFIG_SCHEMA = {
schema: {
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
properties: {
meta: {
type: "object",
properties: {
lastTouchedVersion: { type: "string" },
lastTouchedAt: { anyOf: [{ type: "string" }, {}] }
},
additionalProperties: false
},
env: {
type: "object",
properties: {
shellEnv: {
type: "object",
properties: {
enabled: { type: "boolean" },
timeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
vars: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
}
},
additionalProperties: { type: "string" }
},
wizard: {
type: "object",
properties: {
lastRunAt: { type: "string" },
lastRunVersion: { type: "string" },
lastRunCommit: { type: "string" },
lastRunCommand: { type: "string" },
lastRunMode: { anyOf: [{
type: "string",
const: "local"
}, {
type: "string",
const: "remote"
}] }
},
additionalProperties: false
},
diagnostics: {
type: "object",
properties: {
enabled: { type: "boolean" },
flags: {
type: "array",
items: { type: "string" }
},
stuckSessionWarnMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
otel: {
type: "object",
properties: {
enabled: { type: "boolean" },
endpoint: { type: "string" },
protocol: { anyOf: [{
type: "string",
const: "http/protobuf"
}, {
type: "string",
const: "grpc"
}] },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
serviceName: { type: "string" },
traces: { type: "boolean" },
metrics: { type: "boolean" },
logs: { type: "boolean" },
sampleRate: {
type: "number",
minimum: 0,
maximum: 1
},
flushIntervalMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
cacheTrace: {
type: "object",
properties: {
enabled: { type: "boolean" },
filePath: { type: "string" },
includeMessages: { type: "boolean" },
includePrompt: { type: "boolean" },
includeSystem: { type: "boolean" }
},
additionalProperties: false
}
},
additionalProperties: false
},
logging: {
type: "object",
properties: {
level: { anyOf: [
{
type: "string",
const: "silent"
},
{
type: "string",
const: "fatal"
},
{
type: "string",
const: "error"
},
{
type: "string",
const: "warn"
},
{
type: "string",
const: "info"
},
{
type: "string",
const: "debug"
},
{
type: "string",
const: "trace"
}
] },
file: { type: "string" },
maxFileBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
consoleLevel: { anyOf: [
{
type: "string",
const: "silent"
},
{
type: "string",
const: "fatal"
},
{
type: "string",
const: "error"
},
{
type: "string",
const: "warn"
},
{
type: "string",
const: "info"
},
{
type: "string",
const: "debug"
},
{
type: "string",
const: "trace"
}
] },
consoleStyle: { anyOf: [
{
type: "string",
const: "pretty"
},
{
type: "string",
const: "compact"
},
{
type: "string",
const: "json"
}
] },
redactSensitive: { anyOf: [{
type: "string",
const: "off"
}, {
type: "string",
const: "tools"
}] },
redactPatterns: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
cli: {
type: "object",
properties: { banner: {
type: "object",
properties: { taglineMode: { anyOf: [
{
type: "string",
const: "random"
},
{
type: "string",
const: "default"
},
{
type: "string",
const: "off"
}
] } },
additionalProperties: false
} },
additionalProperties: false
},
update: {
type: "object",
properties: {
channel: { anyOf: [
{
type: "string",
const: "stable"
},
{
type: "string",
const: "beta"
},
{
type: "string",
const: "dev"
}
] },
checkOnStart: { type: "boolean" },
auto: {
type: "object",
properties: {
enabled: { type: "boolean" },
stableDelayHours: {
type: "number",
minimum: 0,
maximum: 168
},
stableJitterHours: {
type: "number",
minimum: 0,
maximum: 168
},
betaCheckIntervalHours: {
type: "number",
exclusiveMinimum: 0,
maximum: 24
}
},
additionalProperties: false
}
},
additionalProperties: false
},
browser: {
type: "object",
properties: {
enabled: { type: "boolean" },
evaluateEnabled: { type: "boolean" },
cdpUrl: { type: "string" },
remoteCdpTimeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
remoteCdpHandshakeTimeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
color: { type: "string" },
executablePath: { type: "string" },
headless: { type: "boolean" },
noSandbox: { type: "boolean" },
attachOnly: { type: "boolean" },
cdpPortRangeStart: {
type: "integer",
minimum: 1,
maximum: 65535
},
defaultProfile: { type: "string" },
snapshotDefaults: {
type: "object",
properties: { mode: {
type: "string",
const: "efficient"
} },
additionalProperties: false
},
ssrfPolicy: {
type: "object",
properties: {
allowPrivateNetwork: { type: "boolean" },
dangerouslyAllowPrivateNetwork: { type: "boolean" },
allowedHostnames: {
type: "array",
items: { type: "string" }
},
hostnameAllowlist: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
profiles: {
type: "object",
propertyNames: {
type: "string",
pattern: "^[a-z0-9-]+$"
},
additionalProperties: {
type: "object",
properties: {
cdpPort: {
type: "integer",
minimum: 1,
maximum: 65535
},
cdpUrl: { type: "string" },
userDataDir: { type: "string" },
driver: { anyOf: [
{
type: "string",
const: "openclaw"
},
{
type: "string",
const: "clawd"
},
{
type: "string",
const: "existing-session"
}
] },
attachOnly: { type: "boolean" },
color: {
type: "string",
pattern: "^#?[0-9a-fA-F]{6}$"
}
},
required: ["color"],
additionalProperties: false
}
},
extraArgs: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
ui: {
type: "object",
properties: {
seamColor: {
type: "string",
pattern: "^#?[0-9a-fA-F]{6}$"
},
assistant: {
type: "object",
properties: {
name: {
type: "string",
maxLength: 50
},
avatar: {
type: "string",
maxLength: 200
}
},
additionalProperties: false
}
},
additionalProperties: false
},
secrets: {
type: "object",
properties: {
providers: {
type: "object",
properties: {},
additionalProperties: { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
allowlist: {
maxItems: 256,
type: "array",
items: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
}
},
required: ["source"],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
path: {
type: "string",
minLength: 1
},
mode: { anyOf: [{
type: "string",
const: "singleValue"
}, {
type: "string",
const: "json"
}] },
timeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 12e4
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 20971520
}
},
required: ["source", "path"],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
command: {
type: "string",
minLength: 1
},
args: {
maxItems: 128,
type: "array",
items: {
type: "string",
maxLength: 1024
}
},
timeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 12e4
},
noOutputTimeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 12e4
},
maxOutputBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 20971520
},
jsonOnly: { type: "boolean" },
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
passEnv: {
maxItems: 128,
type: "array",
items: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
trustedDirs: {
maxItems: 64,
type: "array",
items: {
type: "string",
minLength: 1
}
},
allowInsecurePath: { type: "boolean" },
allowSymlinkCommand: { type: "boolean" }
},
required: ["source", "command"],
additionalProperties: false
}
] }
},
defaults: {
type: "object",
properties: {
env: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
file: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
exec: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
}
},
additionalProperties: false
},
resolution: {
type: "object",
properties: {
maxProviderConcurrency: {
type: "integer",
exclusiveMinimum: 0,
maximum: 16
},
maxRefsPerProvider: {
type: "integer",
exclusiveMinimum: 0,
maximum: 4096
},
maxBatchBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 5242880
}
},
additionalProperties: false
}
},
additionalProperties: false
},
auth: {
type: "object",
properties: {
profiles: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
provider: { type: "string" },
mode: { anyOf: [
{
type: "string",
const: "api_key"
},
{
type: "string",
const: "oauth"
},
{
type: "string",
const: "token"
}
] },
email: { type: "string" }
},
required: ["provider", "mode"],
additionalProperties: false
}
},
order: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "array",
items: { type: "string" }
}
},
cooldowns: {
type: "object",
properties: {
billingBackoffHours: {
type: "number",
exclusiveMinimum: 0
},
billingBackoffHoursByProvider: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "number",
exclusiveMinimum: 0
}
},
billingMaxHours: {
type: "number",
exclusiveMinimum: 0
},
failureWindowHours: {
type: "number",
exclusiveMinimum: 0
}
},
additionalProperties: false
}
},
additionalProperties: false
},
acp: {
type: "object",
properties: {
enabled: { type: "boolean" },
dispatch: {
type: "object",
properties: { enabled: { type: "boolean" } },
additionalProperties: false
},
backend: { type: "string" },
defaultAgent: { type: "string" },
allowedAgents: {
type: "array",
items: { type: "string" }
},
maxConcurrentSessions: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
stream: {
type: "object",
properties: {
coalesceIdleMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxChunkChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
repeatSuppression: { type: "boolean" },
deliveryMode: { anyOf: [{
type: "string",
const: "live"
}, {
type: "string",
const: "final_only"
}] },
hiddenBoundarySeparator: { anyOf: [
{
type: "string",
const: "none"
},
{
type: "string",
const: "space"
},
{
type: "string",
const: "newline"
},
{
type: "string",
const: "paragraph"
}
] },
maxOutputChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxSessionUpdateChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
tagVisibility: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "boolean" }
}
},
additionalProperties: false
},
runtime: {
type: "object",
properties: {
ttlMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
installCommand: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
models: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "merge"
}, {
type: "string",
const: "replace"
}] },
providers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
baseUrl: {
type: "string",
minLength: 1
},
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
auth: { anyOf: [
{
type: "string",
const: "api-key"
},
{
type: "string",
const: "aws-sdk"
},
{
type: "string",
const: "oauth"
},
{
type: "string",
const: "token"
}
] },
api: {
type: "string",
enum: [
"openai-completions",
"openai-responses",
"openai-codex-responses",
"anthropic-messages",
"google-generative-ai",
"github-copilot",
"bedrock-converse-stream",
"ollama"
]
},
injectNumCtxForOpenAICompat: { type: "boolean" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] }
},
authHeader: { type: "boolean" },
models: {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "string",
minLength: 1
},
name: {
type: "string",
minLength: 1
},
api: {
type: "string",
enum: [
"openai-completions",
"openai-responses",
"openai-codex-responses",
"anthropic-messages",
"google-generative-ai",
"github-copilot",
"bedrock-converse-stream",
"ollama"
]
},
reasoning: { type: "boolean" },
input: {
type: "array",
items: { anyOf: [{
type: "string",
const: "text"
}, {
type: "string",
const: "image"
}] }
},
cost: {
type: "object",
properties: {
input: { type: "number" },
output: { type: "number" },
cacheRead: { type: "number" },
cacheWrite: { type: "number" }
},
additionalProperties: false
},
contextWindow: {
type: "number",
exclusiveMinimum: 0
},
maxTokens: {
type: "number",
exclusiveMinimum: 0
},
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
compat: {
type: "object",
properties: {
supportsStore: { type: "boolean" },
supportsDeveloperRole: { type: "boolean" },
supportsReasoningEffort: { type: "boolean" },
supportsUsageInStreaming: { type: "boolean" },
supportsTools: { type: "boolean" },
supportsStrictMode: { type: "boolean" },
maxTokensField: { anyOf: [{
type: "string",
const: "max_completion_tokens"
}, {
type: "string",
const: "max_tokens"
}] },
thinkingFormat: { anyOf: [
{
type: "string",
const: "openai"
},
{
type: "string",
const: "openrouter"
},
{
type: "string",
const: "zai"
},
{
type: "string",
const: "qwen"
},
{
type: "string",
const: "qwen-chat-template"
}
] },
requiresToolResultName: { type: "boolean" },
requiresAssistantAfterToolResult: { type: "boolean" },
requiresThinkingAsText: { type: "boolean" },
toolSchemaProfile: {
type: "string",
const: "xai"
},
nativeWebSearchTool: { type: "boolean" },
toolCallArgumentsEncoding: {
type: "string",
const: "html-entities"
},
requiresMistralToolIds: { type: "boolean" },
requiresOpenAiAnthropicToolPayload: { type: "boolean" }
},
additionalProperties: false
}
},
required: ["id", "name"],
additionalProperties: false
}
}
},
required: ["baseUrl", "models"],
additionalProperties: false
}
},
bedrockDiscovery: {
type: "object",
properties: {
enabled: { type: "boolean" },
region: { type: "string" },
providerFilter: {
type: "array",
items: { type: "string" }
},
refreshInterval: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
defaultContextWindow: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
defaultMaxTokens: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
nodeHost: {
type: "object",
properties: { browserProxy: {
type: "object",
properties: {
enabled: { type: "boolean" },
allowProfiles: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
} },
additionalProperties: false
},
agents: {
type: "object",
properties: {
defaults: {
type: "object",
properties: {
model: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
imageModel: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
imageGenerationModel: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
pdfModel: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
pdfMaxBytesMb: {
type: "number",
exclusiveMinimum: 0
},
pdfMaxPages: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
models: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
alias: { type: "string" },
params: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {}
},
streaming: { type: "boolean" }
},
additionalProperties: false
}
},
workspace: { type: "string" },
repoRoot: { type: "string" },
skipBootstrap: { type: "boolean" },
bootstrapMaxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
bootstrapTotalMaxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
bootstrapPromptTruncationWarning: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "once"
},
{
type: "string",
const: "always"
}
] },
userTimezone: { type: "string" },
timeFormat: { anyOf: [
{
type: "string",
const: "auto"
},
{
type: "string",
const: "12"
},
{
type: "string",
const: "24"
}
] },
envelopeTimezone: { type: "string" },
envelopeTimestamp: { anyOf: [{
type: "string",
const: "on"
}, {
type: "string",
const: "off"
}] },
envelopeElapsed: { anyOf: [{
type: "string",
const: "on"
}, {
type: "string",
const: "off"
}] },
contextTokens: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cliBackends: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
command: { type: "string" },
args: {
type: "array",
items: { type: "string" }
},
output: { anyOf: [
{
type: "string",
const: "json"
},
{
type: "string",
const: "text"
},
{
type: "string",
const: "jsonl"
}
] },
resumeOutput: { anyOf: [
{
type: "string",
const: "json"
},
{
type: "string",
const: "text"
},
{
type: "string",
const: "jsonl"
}
] },
input: { anyOf: [{
type: "string",
const: "arg"
}, {
type: "string",
const: "stdin"
}] },
maxPromptArgChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
clearEnv: {
type: "array",
items: { type: "string" }
},
modelArg: { type: "string" },
modelAliases: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
sessionArg: { type: "string" },
sessionArgs: {
type: "array",
items: { type: "string" }
},
resumeArgs: {
type: "array",
items: { type: "string" }
},
sessionMode: { anyOf: [
{
type: "string",
const: "always"
},
{
type: "string",
const: "existing"
},
{
type: "string",
const: "none"
}
] },
sessionIdFields: {
type: "array",
items: { type: "string" }
},
systemPromptArg: { type: "string" },
systemPromptMode: { anyOf: [{
type: "string",
const: "append"
}, {
type: "string",
const: "replace"
}] },
systemPromptWhen: { anyOf: [
{
type: "string",
const: "first"
},
{
type: "string",
const: "always"
},
{
type: "string",
const: "never"
}
] },
imageArg: { type: "string" },
imageMode: { anyOf: [{
type: "string",
const: "repeat"
}, {
type: "string",
const: "list"
}] },
serialize: { type: "boolean" },
reliability: {
type: "object",
properties: { watchdog: {
type: "object",
properties: {
fresh: {
type: "object",
properties: {
noOutputTimeoutMs: {
type: "integer",
minimum: 1e3,
maximum: 9007199254740991
},
noOutputTimeoutRatio: {
type: "number",
minimum: .05,
maximum: .95
},
minMs: {
type: "integer",
minimum: 1e3,
maximum: 9007199254740991
},
maxMs: {
type: "integer",
minimum: 1e3,
maximum: 9007199254740991
}
},
additionalProperties: false
},
resume: {
type: "object",
properties: {
noOutputTimeoutMs: {
type: "integer",
minimum: 1e3,
maximum: 9007199254740991
},
noOutputTimeoutRatio: {
type: "number",
minimum: .05,
maximum: .95
},
minMs: {
type: "integer",
minimum: 1e3,
maximum: 9007199254740991
},
maxMs: {
type: "integer",
minimum: 1e3,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
} },
additionalProperties: false
}
},
required: ["command"],
additionalProperties: false
}
},
memorySearch: {
type: "object",
properties: {
enabled: { type: "boolean" },
sources: {
type: "array",
items: { anyOf: [{
type: "string",
const: "memory"
}, {
type: "string",
const: "sessions"
}] }
},
extraPaths: {
type: "array",
items: { type: "string" }
},
multimodal: {
type: "object",
properties: {
enabled: { type: "boolean" },
modalities: {
type: "array",
items: { anyOf: [
{
type: "string",
const: "image"
},
{
type: "string",
const: "audio"
},
{
type: "string",
const: "all"
}
] }
},
maxFileBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
experimental: {
type: "object",
properties: { sessionMemory: { type: "boolean" } },
additionalProperties: false
},
provider: { anyOf: [
{
type: "string",
const: "openai"
},
{
type: "string",
const: "local"
},
{
type: "string",
const: "gemini"
},
{
type: "string",
const: "voyage"
},
{
type: "string",
const: "mistral"
},
{
type: "string",
const: "ollama"
}
] },
remote: {
type: "object",
properties: {
baseUrl: { type: "string" },
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
batch: {
type: "object",
properties: {
enabled: { type: "boolean" },
wait: { type: "boolean" },
concurrency: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
pollIntervalMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
timeoutMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
fallback: { anyOf: [
{
type: "string",
const: "openai"
},
{
type: "string",
const: "gemini"
},
{
type: "string",
const: "local"
},
{
type: "string",
const: "voyage"
},
{
type: "string",
const: "mistral"
},
{
type: "string",
const: "ollama"
},
{
type: "string",
const: "none"
}
] },
model: { type: "string" },
outputDimensionality: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
local: {
type: "object",
properties: {
modelPath: { type: "string" },
modelCacheDir: { type: "string" }
},
additionalProperties: false
},
store: {
type: "object",
properties: {
driver: {
type: "string",
const: "sqlite"
},
path: { type: "string" },
vector: {
type: "object",
properties: {
enabled: { type: "boolean" },
extensionPath: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
chunking: {
type: "object",
properties: {
tokens: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
overlap: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
sync: {
type: "object",
properties: {
onSessionStart: { type: "boolean" },
onSearch: { type: "boolean" },
watch: { type: "boolean" },
watchDebounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
intervalMinutes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
sessions: {
type: "object",
properties: {
deltaBytes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
deltaMessages: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
postCompactionForce: { type: "boolean" }
},
additionalProperties: false
}
},
additionalProperties: false
},
query: {
type: "object",
properties: {
maxResults: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
minScore: {
type: "number",
minimum: 0,
maximum: 1
},
hybrid: {
type: "object",
properties: {
enabled: { type: "boolean" },
vectorWeight: {
type: "number",
minimum: 0,
maximum: 1
},
textWeight: {
type: "number",
minimum: 0,
maximum: 1
},
candidateMultiplier: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
mmr: {
type: "object",
properties: {
enabled: { type: "boolean" },
lambda: {
type: "number",
minimum: 0,
maximum: 1
}
},
additionalProperties: false
},
temporalDecay: {
type: "object",
properties: {
enabled: { type: "boolean" },
halfLifeDays: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
},
cache: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxEntries: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
contextPruning: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "off"
}, {
type: "string",
const: "cache-ttl"
}] },
ttl: { type: "string" },
keepLastAssistants: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
softTrimRatio: {
type: "number",
minimum: 0,
maximum: 1
},
hardClearRatio: {
type: "number",
minimum: 0,
maximum: 1
},
minPrunableToolChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
tools: {
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
softTrim: {
type: "object",
properties: {
maxChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
headChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
tailChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
hardClear: {
type: "object",
properties: {
enabled: { type: "boolean" },
placeholder: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
compaction: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "default"
}, {
type: "string",
const: "safeguard"
}] },
reserveTokens: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
keepRecentTokens: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
reserveTokensFloor: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxHistoryShare: {
type: "number",
minimum: .1,
maximum: .9
},
customInstructions: { type: "string" },
identifierPolicy: { anyOf: [
{
type: "string",
const: "strict"
},
{
type: "string",
const: "off"
},
{
type: "string",
const: "custom"
}
] },
identifierInstructions: { type: "string" },
recentTurnsPreserve: {
type: "integer",
minimum: 0,
maximum: 12
},
qualityGuard: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxRetries: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
postIndexSync: {
type: "string",
enum: [
"off",
"async",
"await"
]
},
postCompactionSections: {
type: "array",
items: { type: "string" }
},
model: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
memoryFlush: {
type: "object",
properties: {
enabled: { type: "boolean" },
softThresholdTokens: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
forceFlushTranscriptBytes: { anyOf: [{
type: "integer",
minimum: 0,
maximum: 9007199254740991
}, { type: "string" }] },
prompt: { type: "string" },
systemPrompt: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
embeddedPi: {
type: "object",
properties: { projectSettingsPolicy: { anyOf: [
{
type: "string",
const: "trusted"
},
{
type: "string",
const: "sanitize"
},
{
type: "string",
const: "ignore"
}
] } },
additionalProperties: false
},
thinkingDefault: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "minimal"
},
{
type: "string",
const: "low"
},
{
type: "string",
const: "medium"
},
{
type: "string",
const: "high"
},
{
type: "string",
const: "xhigh"
},
{
type: "string",
const: "adaptive"
}
] },
verboseDefault: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "on"
},
{
type: "string",
const: "full"
}
] },
elevatedDefault: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "on"
},
{
type: "string",
const: "ask"
},
{
type: "string",
const: "full"
}
] },
blockStreamingDefault: { anyOf: [{
type: "string",
const: "off"
}, {
type: "string",
const: "on"
}] },
blockStreamingBreak: { anyOf: [{
type: "string",
const: "text_end"
}, {
type: "string",
const: "message_end"
}] },
blockStreamingChunk: {
type: "object",
properties: {
minChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
breakPreference: { anyOf: [
{
type: "string",
const: "paragraph"
},
{
type: "string",
const: "newline"
},
{
type: "string",
const: "sentence"
}
] }
},
additionalProperties: false
},
blockStreamingCoalesce: {
type: "object",
properties: {
minChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
idleMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
humanDelay: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "natural"
},
{
type: "string",
const: "custom"
}
] },
minMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
mediaMaxMb: {
type: "number",
exclusiveMinimum: 0
},
imageMaxDimensionPx: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
typingIntervalSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
typingMode: { anyOf: [
{
type: "string",
const: "never"
},
{
type: "string",
const: "instant"
},
{
type: "string",
const: "thinking"
},
{
type: "string",
const: "message"
}
] },
heartbeat: {
type: "object",
properties: {
every: { type: "string" },
activeHours: {
type: "object",
properties: {
start: { type: "string" },
end: { type: "string" },
timezone: { type: "string" }
},
additionalProperties: false
},
model: { type: "string" },
session: { type: "string" },
includeReasoning: { type: "boolean" },
target: { type: "string" },
directPolicy: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "block"
}] },
to: { type: "string" },
accountId: { type: "string" },
prompt: { type: "string" },
ackMaxChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
suppressToolErrorWarnings: { type: "boolean" },
lightContext: { type: "boolean" },
isolatedSession: { type: "boolean" }
},
additionalProperties: false
},
maxConcurrent: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
subagents: {
type: "object",
properties: {
maxConcurrent: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxSpawnDepth: {
description: "Maximum nesting depth for sub-agent spawning. 1 = no nesting (default), 2 = sub-agents can spawn sub-sub-agents.",
type: "integer",
minimum: 1,
maximum: 5
},
maxChildrenPerAgent: {
description: "Maximum number of active children a single agent session can spawn (default: 5).",
type: "integer",
minimum: 1,
maximum: 20
},
archiveAfterMinutes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
model: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
thinking: { type: "string" },
runTimeoutSeconds: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
announceTimeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
sandbox: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "non-main"
},
{
type: "string",
const: "all"
}
] },
backend: {
type: "string",
minLength: 1
},
workspaceAccess: { anyOf: [
{
type: "string",
const: "none"
},
{
type: "string",
const: "ro"
},
{
type: "string",
const: "rw"
}
] },
sessionToolsVisibility: { anyOf: [{
type: "string",
const: "spawned"
}, {
type: "string",
const: "all"
}] },
scope: { anyOf: [
{
type: "string",
const: "session"
},
{
type: "string",
const: "agent"
},
{
type: "string",
const: "shared"
}
] },
perSession: { type: "boolean" },
workspaceRoot: { type: "string" },
docker: {
type: "object",
properties: {
image: { type: "string" },
containerPrefix: { type: "string" },
workdir: { type: "string" },
readOnlyRoot: { type: "boolean" },
tmpfs: {
type: "array",
items: { type: "string" }
},
network: { type: "string" },
user: { type: "string" },
capDrop: {
type: "array",
items: { type: "string" }
},
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
setupCommand: {},
pidsLimit: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
memory: { anyOf: [{ type: "string" }, { type: "number" }] },
memorySwap: { anyOf: [{ type: "string" }, { type: "number" }] },
cpus: {
type: "number",
exclusiveMinimum: 0
},
ulimits: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{
type: "object",
properties: {
soft: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
hard: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
] }
},
seccompProfile: { type: "string" },
apparmorProfile: { type: "string" },
dns: {
type: "array",
items: { type: "string" }
},
extraHosts: {
type: "array",
items: { type: "string" }
},
binds: {
type: "array",
items: { type: "string" }
},
dangerouslyAllowReservedContainerTargets: { type: "boolean" },
dangerouslyAllowExternalBindSources: { type: "boolean" },
dangerouslyAllowContainerNamespaceJoin: { type: "boolean" }
},
additionalProperties: false
},
ssh: {
type: "object",
properties: {
target: {
type: "string",
minLength: 1
},
command: {
type: "string",
minLength: 1
},
workspaceRoot: {
type: "string",
minLength: 1
},
strictHostKeyChecking: { type: "boolean" },
updateHostKeys: { type: "boolean" },
identityFile: {
type: "string",
minLength: 1
},
certificateFile: {
type: "string",
minLength: 1
},
knownHostsFile: {
type: "string",
minLength: 1
},
identityData: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
certificateData: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
knownHostsData: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] }
},
additionalProperties: false
},
browser: {
type: "object",
properties: {
enabled: { type: "boolean" },
image: { type: "string" },
containerPrefix: { type: "string" },
network: { type: "string" },
cdpPort: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cdpSourceRange: { type: "string" },
vncPort: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
noVncPort: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
headless: { type: "boolean" },
enableNoVnc: { type: "boolean" },
allowHostControl: { type: "boolean" },
autoStart: { type: "boolean" },
autoStartTimeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
binds: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
prune: {
type: "object",
properties: {
idleHours: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxAgeDays: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
},
list: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
default: { type: "boolean" },
name: { type: "string" },
workspace: { type: "string" },
agentDir: { type: "string" },
model: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
thinkingDefault: {
type: "string",
enum: [
"off",
"minimal",
"low",
"medium",
"high",
"xhigh",
"adaptive"
]
},
reasoningDefault: {
type: "string",
enum: [
"on",
"off",
"stream"
]
},
fastModeDefault: { type: "boolean" },
skills: {
type: "array",
items: { type: "string" }
},
memorySearch: {
type: "object",
properties: {
enabled: { type: "boolean" },
sources: {
type: "array",
items: { anyOf: [{
type: "string",
const: "memory"
}, {
type: "string",
const: "sessions"
}] }
},
extraPaths: {
type: "array",
items: { type: "string" }
},
multimodal: {
type: "object",
properties: {
enabled: { type: "boolean" },
modalities: {
type: "array",
items: { anyOf: [
{
type: "string",
const: "image"
},
{
type: "string",
const: "audio"
},
{
type: "string",
const: "all"
}
] }
},
maxFileBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
experimental: {
type: "object",
properties: { sessionMemory: { type: "boolean" } },
additionalProperties: false
},
provider: { anyOf: [
{
type: "string",
const: "openai"
},
{
type: "string",
const: "local"
},
{
type: "string",
const: "gemini"
},
{
type: "string",
const: "voyage"
},
{
type: "string",
const: "mistral"
},
{
type: "string",
const: "ollama"
}
] },
remote: {
type: "object",
properties: {
baseUrl: { type: "string" },
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
batch: {
type: "object",
properties: {
enabled: { type: "boolean" },
wait: { type: "boolean" },
concurrency: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
pollIntervalMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
timeoutMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
fallback: { anyOf: [
{
type: "string",
const: "openai"
},
{
type: "string",
const: "gemini"
},
{
type: "string",
const: "local"
},
{
type: "string",
const: "voyage"
},
{
type: "string",
const: "mistral"
},
{
type: "string",
const: "ollama"
},
{
type: "string",
const: "none"
}
] },
model: { type: "string" },
outputDimensionality: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
local: {
type: "object",
properties: {
modelPath: { type: "string" },
modelCacheDir: { type: "string" }
},
additionalProperties: false
},
store: {
type: "object",
properties: {
driver: {
type: "string",
const: "sqlite"
},
path: { type: "string" },
vector: {
type: "object",
properties: {
enabled: { type: "boolean" },
extensionPath: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
chunking: {
type: "object",
properties: {
tokens: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
overlap: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
sync: {
type: "object",
properties: {
onSessionStart: { type: "boolean" },
onSearch: { type: "boolean" },
watch: { type: "boolean" },
watchDebounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
intervalMinutes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
sessions: {
type: "object",
properties: {
deltaBytes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
deltaMessages: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
postCompactionForce: { type: "boolean" }
},
additionalProperties: false
}
},
additionalProperties: false
},
query: {
type: "object",
properties: {
maxResults: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
minScore: {
type: "number",
minimum: 0,
maximum: 1
},
hybrid: {
type: "object",
properties: {
enabled: { type: "boolean" },
vectorWeight: {
type: "number",
minimum: 0,
maximum: 1
},
textWeight: {
type: "number",
minimum: 0,
maximum: 1
},
candidateMultiplier: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
mmr: {
type: "object",
properties: {
enabled: { type: "boolean" },
lambda: {
type: "number",
minimum: 0,
maximum: 1
}
},
additionalProperties: false
},
temporalDecay: {
type: "object",
properties: {
enabled: { type: "boolean" },
halfLifeDays: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
},
cache: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxEntries: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
humanDelay: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "natural"
},
{
type: "string",
const: "custom"
}
] },
minMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
heartbeat: {
type: "object",
properties: {
every: { type: "string" },
activeHours: {
type: "object",
properties: {
start: { type: "string" },
end: { type: "string" },
timezone: { type: "string" }
},
additionalProperties: false
},
model: { type: "string" },
session: { type: "string" },
includeReasoning: { type: "boolean" },
target: { type: "string" },
directPolicy: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "block"
}] },
to: { type: "string" },
accountId: { type: "string" },
prompt: { type: "string" },
ackMaxChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
suppressToolErrorWarnings: { type: "boolean" },
lightContext: { type: "boolean" },
isolatedSession: { type: "boolean" }
},
additionalProperties: false
},
identity: {
type: "object",
properties: {
name: { type: "string" },
theme: { type: "string" },
emoji: { type: "string" },
avatar: { type: "string" }
},
additionalProperties: false
},
groupChat: {
type: "object",
properties: {
mentionPatterns: {
type: "array",
items: { type: "string" }
},
historyLimit: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
subagents: {
type: "object",
properties: {
allowAgents: {
type: "array",
items: { type: "string" }
},
model: { anyOf: [{ type: "string" }, {
type: "object",
properties: {
primary: { type: "string" },
fallbacks: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}] },
thinking: { type: "string" }
},
additionalProperties: false
},
sandbox: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "non-main"
},
{
type: "string",
const: "all"
}
] },
backend: {
type: "string",
minLength: 1
},
workspaceAccess: { anyOf: [
{
type: "string",
const: "none"
},
{
type: "string",
const: "ro"
},
{
type: "string",
const: "rw"
}
] },
sessionToolsVisibility: { anyOf: [{
type: "string",
const: "spawned"
}, {
type: "string",
const: "all"
}] },
scope: { anyOf: [
{
type: "string",
const: "session"
},
{
type: "string",
const: "agent"
},
{
type: "string",
const: "shared"
}
] },
perSession: { type: "boolean" },
workspaceRoot: { type: "string" },
docker: {
type: "object",
properties: {
image: { type: "string" },
containerPrefix: { type: "string" },
workdir: { type: "string" },
readOnlyRoot: { type: "boolean" },
tmpfs: {
type: "array",
items: { type: "string" }
},
network: { type: "string" },
user: { type: "string" },
capDrop: {
type: "array",
items: { type: "string" }
},
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
setupCommand: {},
pidsLimit: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
memory: { anyOf: [{ type: "string" }, { type: "number" }] },
memorySwap: { anyOf: [{ type: "string" }, { type: "number" }] },
cpus: {
type: "number",
exclusiveMinimum: 0
},
ulimits: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{
type: "object",
properties: {
soft: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
hard: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
] }
},
seccompProfile: { type: "string" },
apparmorProfile: { type: "string" },
dns: {
type: "array",
items: { type: "string" }
},
extraHosts: {
type: "array",
items: { type: "string" }
},
binds: {
type: "array",
items: { type: "string" }
},
dangerouslyAllowReservedContainerTargets: { type: "boolean" },
dangerouslyAllowExternalBindSources: { type: "boolean" },
dangerouslyAllowContainerNamespaceJoin: { type: "boolean" }
},
additionalProperties: false
},
ssh: {
type: "object",
properties: {
target: {
type: "string",
minLength: 1
},
command: {
type: "string",
minLength: 1
},
workspaceRoot: {
type: "string",
minLength: 1
},
strictHostKeyChecking: { type: "boolean" },
updateHostKeys: { type: "boolean" },
identityFile: {
type: "string",
minLength: 1
},
certificateFile: {
type: "string",
minLength: 1
},
knownHostsFile: {
type: "string",
minLength: 1
},
identityData: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
certificateData: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
knownHostsData: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] }
},
additionalProperties: false
},
browser: {
type: "object",
properties: {
enabled: { type: "boolean" },
image: { type: "string" },
containerPrefix: { type: "string" },
network: { type: "string" },
cdpPort: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cdpSourceRange: { type: "string" },
vncPort: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
noVncPort: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
headless: { type: "boolean" },
enableNoVnc: { type: "boolean" },
allowHostControl: { type: "boolean" },
autoStart: { type: "boolean" },
autoStartTimeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
binds: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
prune: {
type: "object",
properties: {
idleHours: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxAgeDays: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
params: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {}
},
tools: {
type: "object",
properties: {
profile: { anyOf: [
{
type: "string",
const: "minimal"
},
{
type: "string",
const: "coding"
},
{
type: "string",
const: "messaging"
},
{
type: "string",
const: "full"
}
] },
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
},
byProvider: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
},
profile: { anyOf: [
{
type: "string",
const: "minimal"
},
{
type: "string",
const: "coding"
},
{
type: "string",
const: "messaging"
},
{
type: "string",
const: "full"
}
] }
},
additionalProperties: false
}
},
elevated: {
type: "object",
properties: {
enabled: { type: "boolean" },
allowFrom: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "array",
items: { anyOf: [{ type: "string" }, { type: "number" }] }
}
}
},
additionalProperties: false
},
exec: {
type: "object",
properties: {
host: {
type: "string",
enum: [
"sandbox",
"gateway",
"node"
]
},
security: {
type: "string",
enum: [
"deny",
"allowlist",
"full"
]
},
ask: {
type: "string",
enum: [
"off",
"on-miss",
"always"
]
},
node: { type: "string" },
pathPrepend: {
type: "array",
items: { type: "string" }
},
safeBins: {
type: "array",
items: { type: "string" }
},
strictInlineEval: { type: "boolean" },
safeBinTrustedDirs: {
type: "array",
items: { type: "string" }
},
safeBinProfiles: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
minPositional: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxPositional: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
allowedValueFlags: {
type: "array",
items: { type: "string" }
},
deniedFlags: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}
},
backgroundMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
timeoutSec: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cleanupMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
notifyOnExit: { type: "boolean" },
notifyOnExitEmptySuccess: { type: "boolean" },
applyPatch: {
type: "object",
properties: {
enabled: { type: "boolean" },
workspaceOnly: { type: "boolean" },
allowModels: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
approvalRunningNoticeMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
fs: {
type: "object",
properties: { workspaceOnly: { type: "boolean" } },
additionalProperties: false
},
loopDetection: {
type: "object",
properties: {
enabled: { type: "boolean" },
historySize: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
warningThreshold: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
criticalThreshold: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
globalCircuitBreakerThreshold: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
detectors: {
type: "object",
properties: {
genericRepeat: { type: "boolean" },
knownPollNoProgress: { type: "boolean" },
pingPong: { type: "boolean" }
},
additionalProperties: false
}
},
additionalProperties: false
},
sandbox: {
type: "object",
properties: { tools: {
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
} },
additionalProperties: false
}
},
additionalProperties: false
},
runtime: { anyOf: [{
type: "object",
properties: { type: {
type: "string",
const: "embedded"
} },
required: ["type"],
additionalProperties: false
}, {
type: "object",
properties: {
type: {
type: "string",
const: "acp"
},
acp: {
type: "object",
properties: {
agent: { type: "string" },
backend: { type: "string" },
mode: {
type: "string",
enum: ["persistent", "oneshot"]
},
cwd: { type: "string" }
},
additionalProperties: false
}
},
required: ["type"],
additionalProperties: false
}] }
},
required: ["id"],
additionalProperties: false
}
}
},
additionalProperties: false
},
tools: {
type: "object",
properties: {
profile: { anyOf: [
{
type: "string",
const: "minimal"
},
{
type: "string",
const: "coding"
},
{
type: "string",
const: "messaging"
},
{
type: "string",
const: "full"
}
] },
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
},
byProvider: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
},
profile: { anyOf: [
{
type: "string",
const: "minimal"
},
{
type: "string",
const: "coding"
},
{
type: "string",
const: "messaging"
},
{
type: "string",
const: "full"
}
] }
},
additionalProperties: false
}
},
web: {
type: "object",
properties: {
search: {
type: "object",
properties: {
enabled: { type: "boolean" },
provider: { type: "string" },
maxResults: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cacheTtlMinutes: {
type: "number",
minimum: 0
},
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
brave: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" },
mode: { type: "string" }
},
additionalProperties: false
},
firecrawl: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" }
},
additionalProperties: false
},
gemini: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" }
},
additionalProperties: false
},
grok: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" },
inlineCitations: { type: "boolean" }
},
additionalProperties: false
},
kimi: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" }
},
additionalProperties: false
},
perplexity: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
fetch: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxCharsCap: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cacheTtlMinutes: {
type: "number",
minimum: 0
},
maxRedirects: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
userAgent: { type: "string" },
readability: { type: "boolean" },
firecrawl: {
type: "object",
properties: {
enabled: { type: "boolean" },
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
onlyMainContent: { type: "boolean" },
maxAgeMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
},
media: {
type: "object",
properties: {
models: {
type: "array",
items: {
type: "object",
properties: {
provider: { type: "string" },
model: { type: "string" },
capabilities: {
type: "array",
items: { anyOf: [
{
type: "string",
const: "image"
},
{
type: "string",
const: "audio"
},
{
type: "string",
const: "video"
}
] }
},
type: { anyOf: [{
type: "string",
const: "provider"
}, {
type: "string",
const: "cli"
}] },
command: { type: "string" },
args: {
type: "array",
items: { type: "string" }
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
profile: { type: "string" },
preferredProfile: { type: "string" }
},
additionalProperties: false
}
},
concurrency: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
image: {
type: "object",
properties: {
enabled: { type: "boolean" },
scope: {
type: "object",
properties: {
default: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
rules: {
type: "array",
items: {
type: "object",
properties: {
action: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
match: {
type: "object",
properties: {
channel: { type: "string" },
chatType: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
keyPrefix: { type: "string" },
rawKeyPrefix: { type: "string" }
},
additionalProperties: false
}
},
required: ["action"],
additionalProperties: false
}
}
},
additionalProperties: false
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
attachments: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "first"
}, {
type: "string",
const: "all"
}] },
maxAttachments: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prefer: { anyOf: [
{
type: "string",
const: "first"
},
{
type: "string",
const: "last"
},
{
type: "string",
const: "path"
},
{
type: "string",
const: "url"
}
] }
},
additionalProperties: false
},
models: {
type: "array",
items: {
type: "object",
properties: {
provider: { type: "string" },
model: { type: "string" },
capabilities: {
type: "array",
items: { anyOf: [
{
type: "string",
const: "image"
},
{
type: "string",
const: "audio"
},
{
type: "string",
const: "video"
}
] }
},
type: { anyOf: [{
type: "string",
const: "provider"
}, {
type: "string",
const: "cli"
}] },
command: { type: "string" },
args: {
type: "array",
items: { type: "string" }
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
profile: { type: "string" },
preferredProfile: { type: "string" }
},
additionalProperties: false
}
},
echoTranscript: { type: "boolean" },
echoFormat: { type: "string" }
},
additionalProperties: false
},
audio: {
type: "object",
properties: {
enabled: { type: "boolean" },
scope: {
type: "object",
properties: {
default: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
rules: {
type: "array",
items: {
type: "object",
properties: {
action: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
match: {
type: "object",
properties: {
channel: { type: "string" },
chatType: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
keyPrefix: { type: "string" },
rawKeyPrefix: { type: "string" }
},
additionalProperties: false
}
},
required: ["action"],
additionalProperties: false
}
}
},
additionalProperties: false
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
attachments: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "first"
}, {
type: "string",
const: "all"
}] },
maxAttachments: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prefer: { anyOf: [
{
type: "string",
const: "first"
},
{
type: "string",
const: "last"
},
{
type: "string",
const: "path"
},
{
type: "string",
const: "url"
}
] }
},
additionalProperties: false
},
models: {
type: "array",
items: {
type: "object",
properties: {
provider: { type: "string" },
model: { type: "string" },
capabilities: {
type: "array",
items: { anyOf: [
{
type: "string",
const: "image"
},
{
type: "string",
const: "audio"
},
{
type: "string",
const: "video"
}
] }
},
type: { anyOf: [{
type: "string",
const: "provider"
}, {
type: "string",
const: "cli"
}] },
command: { type: "string" },
args: {
type: "array",
items: { type: "string" }
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
profile: { type: "string" },
preferredProfile: { type: "string" }
},
additionalProperties: false
}
},
echoTranscript: { type: "boolean" },
echoFormat: { type: "string" }
},
additionalProperties: false
},
video: {
type: "object",
properties: {
enabled: { type: "boolean" },
scope: {
type: "object",
properties: {
default: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
rules: {
type: "array",
items: {
type: "object",
properties: {
action: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
match: {
type: "object",
properties: {
channel: { type: "string" },
chatType: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
keyPrefix: { type: "string" },
rawKeyPrefix: { type: "string" }
},
additionalProperties: false
}
},
required: ["action"],
additionalProperties: false
}
}
},
additionalProperties: false
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
attachments: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "first"
}, {
type: "string",
const: "all"
}] },
maxAttachments: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prefer: { anyOf: [
{
type: "string",
const: "first"
},
{
type: "string",
const: "last"
},
{
type: "string",
const: "path"
},
{
type: "string",
const: "url"
}
] }
},
additionalProperties: false
},
models: {
type: "array",
items: {
type: "object",
properties: {
provider: { type: "string" },
model: { type: "string" },
capabilities: {
type: "array",
items: { anyOf: [
{
type: "string",
const: "image"
},
{
type: "string",
const: "audio"
},
{
type: "string",
const: "video"
}
] }
},
type: { anyOf: [{
type: "string",
const: "provider"
}, {
type: "string",
const: "cli"
}] },
command: { type: "string" },
args: {
type: "array",
items: { type: "string" }
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
prompt: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
language: { type: "string" },
providerOptions: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
}
},
deepgram: {
type: "object",
properties: {
detectLanguage: { type: "boolean" },
punctuate: { type: "boolean" },
smartFormat: { type: "boolean" }
},
additionalProperties: false
},
baseUrl: { type: "string" },
headers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
profile: { type: "string" },
preferredProfile: { type: "string" }
},
additionalProperties: false
}
},
echoTranscript: { type: "boolean" },
echoFormat: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
links: {
type: "object",
properties: {
enabled: { type: "boolean" },
scope: {
type: "object",
properties: {
default: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
rules: {
type: "array",
items: {
type: "object",
properties: {
action: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
match: {
type: "object",
properties: {
channel: { type: "string" },
chatType: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
keyPrefix: { type: "string" },
rawKeyPrefix: { type: "string" }
},
additionalProperties: false
}
},
required: ["action"],
additionalProperties: false
}
}
},
additionalProperties: false
},
maxLinks: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
models: {
type: "array",
items: {
type: "object",
properties: {
type: {
type: "string",
const: "cli"
},
command: {
type: "string",
minLength: 1
},
args: {
type: "array",
items: { type: "string" }
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
required: ["command"],
additionalProperties: false
}
}
},
additionalProperties: false
},
sessions: {
type: "object",
properties: { visibility: {
type: "string",
enum: [
"self",
"tree",
"agent",
"all"
]
} },
additionalProperties: false
},
loopDetection: {
type: "object",
properties: {
enabled: { type: "boolean" },
historySize: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
warningThreshold: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
criticalThreshold: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
globalCircuitBreakerThreshold: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
detectors: {
type: "object",
properties: {
genericRepeat: { type: "boolean" },
knownPollNoProgress: { type: "boolean" },
pingPong: { type: "boolean" }
},
additionalProperties: false
}
},
additionalProperties: false
},
message: {
type: "object",
properties: {
allowCrossContextSend: { type: "boolean" },
crossContext: {
type: "object",
properties: {
allowWithinProvider: { type: "boolean" },
allowAcrossProviders: { type: "boolean" },
marker: {
type: "object",
properties: {
enabled: { type: "boolean" },
prefix: { type: "string" },
suffix: { type: "string" }
},
additionalProperties: false
}
},
additionalProperties: false
},
broadcast: {
type: "object",
properties: { enabled: { type: "boolean" } },
additionalProperties: false
}
},
additionalProperties: false
},
agentToAgent: {
type: "object",
properties: {
enabled: { type: "boolean" },
allow: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
elevated: {
type: "object",
properties: {
enabled: { type: "boolean" },
allowFrom: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "array",
items: { anyOf: [{ type: "string" }, { type: "number" }] }
}
}
},
additionalProperties: false
},
exec: {
type: "object",
properties: {
host: {
type: "string",
enum: [
"sandbox",
"gateway",
"node"
]
},
security: {
type: "string",
enum: [
"deny",
"allowlist",
"full"
]
},
ask: {
type: "string",
enum: [
"off",
"on-miss",
"always"
]
},
node: { type: "string" },
pathPrepend: {
type: "array",
items: { type: "string" }
},
safeBins: {
type: "array",
items: { type: "string" }
},
strictInlineEval: { type: "boolean" },
safeBinTrustedDirs: {
type: "array",
items: { type: "string" }
},
safeBinProfiles: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
minPositional: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxPositional: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
allowedValueFlags: {
type: "array",
items: { type: "string" }
},
deniedFlags: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}
},
backgroundMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
timeoutSec: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
cleanupMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
notifyOnExit: { type: "boolean" },
notifyOnExitEmptySuccess: { type: "boolean" },
applyPatch: {
type: "object",
properties: {
enabled: { type: "boolean" },
workspaceOnly: { type: "boolean" },
allowModels: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}
},
additionalProperties: false
},
fs: {
type: "object",
properties: { workspaceOnly: { type: "boolean" } },
additionalProperties: false
},
subagents: {
type: "object",
properties: { tools: {
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
} },
additionalProperties: false
},
sandbox: {
type: "object",
properties: { tools: {
type: "object",
properties: {
allow: {
type: "array",
items: { type: "string" }
},
alsoAllow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
} },
additionalProperties: false
},
sessions_spawn: {
type: "object",
properties: { attachments: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxTotalBytes: { type: "number" },
maxFiles: { type: "number" },
maxFileBytes: { type: "number" },
retainOnSessionKeep: { type: "boolean" }
},
additionalProperties: false
} },
additionalProperties: false
}
},
additionalProperties: false
},
bindings: {
type: "array",
items: { anyOf: [{
type: "object",
properties: {
type: {
type: "string",
const: "route"
},
agentId: { type: "string" },
comment: { type: "string" },
match: {
type: "object",
properties: {
channel: { type: "string" },
accountId: { type: "string" },
peer: {
type: "object",
properties: {
kind: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
id: { type: "string" }
},
required: ["kind", "id"],
additionalProperties: false
},
guildId: { type: "string" },
teamId: { type: "string" },
roles: {
type: "array",
items: { type: "string" }
}
},
required: ["channel"],
additionalProperties: false
}
},
required: ["agentId", "match"],
additionalProperties: false
}, {
type: "object",
properties: {
type: {
type: "string",
const: "acp"
},
agentId: { type: "string" },
comment: { type: "string" },
match: {
type: "object",
properties: {
channel: { type: "string" },
accountId: { type: "string" },
peer: {
type: "object",
properties: {
kind: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
id: { type: "string" }
},
required: ["kind", "id"],
additionalProperties: false
},
guildId: { type: "string" },
teamId: { type: "string" },
roles: {
type: "array",
items: { type: "string" }
}
},
required: ["channel"],
additionalProperties: false
},
acp: {
type: "object",
properties: {
mode: {
type: "string",
enum: ["persistent", "oneshot"]
},
label: { type: "string" },
cwd: { type: "string" },
backend: { type: "string" }
},
additionalProperties: false
}
},
required: [
"type",
"agentId",
"match"
],
additionalProperties: false
}] }
},
broadcast: {
type: "object",
properties: { strategy: {
type: "string",
enum: ["parallel", "sequential"]
} },
additionalProperties: {
type: "array",
items: { type: "string" }
}
},
audio: {
type: "object",
properties: { transcription: {
type: "object",
properties: {
command: {
type: "array",
items: { type: "string" }
},
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
required: ["command"],
additionalProperties: false
} },
additionalProperties: false
},
media: {
type: "object",
properties: {
preserveFilenames: { type: "boolean" },
ttlHours: {
type: "integer",
minimum: 1,
maximum: 168
}
},
additionalProperties: false
},
messages: {
type: "object",
properties: {
messagePrefix: { type: "string" },
responsePrefix: { type: "string" },
groupChat: {
type: "object",
properties: {
mentionPatterns: {
type: "array",
items: { type: "string" }
},
historyLimit: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
queue: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
byChannel: {
type: "object",
properties: {
whatsapp: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
telegram: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
discord: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
irc: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
slack: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
mattermost: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
signal: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
imessage: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
msteams: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] },
webchat: { anyOf: [
{
type: "string",
const: "steer"
},
{
type: "string",
const: "followup"
},
{
type: "string",
const: "collect"
},
{
type: "string",
const: "steer-backlog"
},
{
type: "string",
const: "steer+backlog"
},
{
type: "string",
const: "queue"
},
{
type: "string",
const: "interrupt"
}
] }
},
additionalProperties: false
},
debounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
debounceMsByChannel: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
cap: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
drop: { anyOf: [
{
type: "string",
const: "old"
},
{
type: "string",
const: "new"
},
{
type: "string",
const: "summarize"
}
] }
},
additionalProperties: false
},
inbound: {
type: "object",
properties: {
debounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
byChannel: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
}
},
additionalProperties: false
},
ackReaction: { type: "string" },
ackReactionScope: {
type: "string",
enum: [
"group-mentions",
"group-all",
"direct",
"all",
"off",
"none"
]
},
removeAckAfterReply: { type: "boolean" },
statusReactions: {
type: "object",
properties: {
enabled: { type: "boolean" },
emojis: {
type: "object",
properties: {
thinking: { type: "string" },
tool: { type: "string" },
coding: { type: "string" },
web: { type: "string" },
done: { type: "string" },
error: { type: "string" },
stallSoft: { type: "string" },
stallHard: { type: "string" },
compacting: { type: "string" }
},
additionalProperties: false
},
timing: {
type: "object",
properties: {
debounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
stallSoftMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
stallHardMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
doneHoldMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
errorHoldMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
suppressToolErrors: { type: "boolean" },
tts: {
type: "object",
properties: {
auto: {
type: "string",
enum: [
"off",
"always",
"inbound",
"tagged"
]
},
enabled: { type: "boolean" },
mode: {
type: "string",
enum: ["final", "all"]
},
provider: {
type: "string",
minLength: 1
},
summaryModel: { type: "string" },
modelOverrides: {
type: "object",
properties: {
enabled: { type: "boolean" },
allowText: { type: "boolean" },
allowProvider: { type: "boolean" },
allowVoice: { type: "boolean" },
allowModelId: { type: "boolean" },
allowVoiceSettings: { type: "boolean" },
allowNormalization: { type: "boolean" },
allowSeed: { type: "boolean" }
},
additionalProperties: false
},
elevenlabs: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
voiceId: { type: "string" },
modelId: { type: "string" },
seed: {
type: "integer",
minimum: 0,
maximum: 4294967295
},
applyTextNormalization: {
type: "string",
enum: [
"auto",
"on",
"off"
]
},
languageCode: { type: "string" },
voiceSettings: {
type: "object",
properties: {
stability: {
type: "number",
minimum: 0,
maximum: 1
},
similarityBoost: {
type: "number",
minimum: 0,
maximum: 1
},
style: {
type: "number",
minimum: 0,
maximum: 1
},
useSpeakerBoost: { type: "boolean" },
speed: {
type: "number",
minimum: .5,
maximum: 2
}
},
additionalProperties: false
}
},
additionalProperties: false
},
openai: {
type: "object",
properties: {
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
baseUrl: { type: "string" },
model: { type: "string" },
voice: { type: "string" },
speed: {
type: "number",
minimum: .25,
maximum: 4
},
instructions: { type: "string" }
},
additionalProperties: false
},
edge: {
type: "object",
properties: {
enabled: { type: "boolean" },
voice: { type: "string" },
lang: { type: "string" },
outputFormat: { type: "string" },
pitch: { type: "string" },
rate: { type: "string" },
volume: { type: "string" },
saveSubtitles: { type: "boolean" },
proxy: { type: "string" },
timeoutMs: {
type: "integer",
minimum: 1e3,
maximum: 12e4
}
},
additionalProperties: false
},
microsoft: {
type: "object",
properties: {
enabled: { type: "boolean" },
voice: { type: "string" },
lang: { type: "string" },
outputFormat: { type: "string" },
pitch: { type: "string" },
rate: { type: "string" },
volume: { type: "string" },
saveSubtitles: { type: "boolean" },
proxy: { type: "string" },
timeoutMs: {
type: "integer",
minimum: 1e3,
maximum: 12e4
}
},
additionalProperties: false
},
prefsPath: { type: "string" },
maxTextLength: {
type: "integer",
minimum: 1,
maximum: 9007199254740991
},
timeoutMs: {
type: "integer",
minimum: 1e3,
maximum: 12e4
}
},
additionalProperties: false
}
},
additionalProperties: false
},
commands: {
default: {
native: "auto",
nativeSkills: "auto",
restart: true,
ownerDisplay: "raw"
},
type: "object",
properties: {
native: {
default: "auto",
anyOf: [{ type: "boolean" }, {
type: "string",
const: "auto"
}]
},
nativeSkills: {
default: "auto",
anyOf: [{ type: "boolean" }, {
type: "string",
const: "auto"
}]
},
text: { type: "boolean" },
bash: { type: "boolean" },
bashForegroundMs: {
type: "integer",
minimum: 0,
maximum: 3e4
},
config: { type: "boolean" },
mcp: { type: "boolean" },
plugins: { type: "boolean" },
debug: { type: "boolean" },
restart: {
default: true,
type: "boolean"
},
useAccessGroups: { type: "boolean" },
ownerAllowFrom: {
type: "array",
items: { anyOf: [{ type: "string" }, { type: "number" }] }
},
ownerDisplay: {
default: "raw",
type: "string",
enum: ["raw", "hash"]
},
ownerDisplaySecret: { type: "string" },
allowFrom: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "array",
items: { anyOf: [{ type: "string" }, { type: "number" }] }
}
}
},
required: [
"native",
"nativeSkills",
"restart",
"ownerDisplay"
],
additionalProperties: false
},
approvals: {
type: "object",
properties: { exec: {
type: "object",
properties: {
enabled: { type: "boolean" },
mode: { anyOf: [
{
type: "string",
const: "session"
},
{
type: "string",
const: "targets"
},
{
type: "string",
const: "both"
}
] },
agentFilter: {
type: "array",
items: { type: "string" }
},
sessionFilter: {
type: "array",
items: { type: "string" }
},
targets: {
type: "array",
items: {
type: "object",
properties: {
channel: {
type: "string",
minLength: 1
},
to: {
type: "string",
minLength: 1
},
accountId: { type: "string" },
threadId: { anyOf: [{ type: "string" }, { type: "number" }] }
},
required: ["channel", "to"],
additionalProperties: false
}
}
},
additionalProperties: false
} },
additionalProperties: false
},
session: {
type: "object",
properties: {
scope: { anyOf: [{
type: "string",
const: "per-sender"
}, {
type: "string",
const: "global"
}] },
dmScope: { anyOf: [
{
type: "string",
const: "main"
},
{
type: "string",
const: "per-peer"
},
{
type: "string",
const: "per-channel-peer"
},
{
type: "string",
const: "per-account-channel-peer"
}
] },
identityLinks: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "array",
items: { type: "string" }
}
},
resetTriggers: {
type: "array",
items: { type: "string" }
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
reset: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "daily"
}, {
type: "string",
const: "idle"
}] },
atHour: {
type: "integer",
minimum: 0,
maximum: 23
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
resetByType: {
type: "object",
properties: {
direct: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "daily"
}, {
type: "string",
const: "idle"
}] },
atHour: {
type: "integer",
minimum: 0,
maximum: 23
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
dm: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "daily"
}, {
type: "string",
const: "idle"
}] },
atHour: {
type: "integer",
minimum: 0,
maximum: 23
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
group: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "daily"
}, {
type: "string",
const: "idle"
}] },
atHour: {
type: "integer",
minimum: 0,
maximum: 23
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
thread: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "daily"
}, {
type: "string",
const: "idle"
}] },
atHour: {
type: "integer",
minimum: 0,
maximum: 23
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
resetByChannel: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
mode: { anyOf: [{
type: "string",
const: "daily"
}, {
type: "string",
const: "idle"
}] },
atHour: {
type: "integer",
minimum: 0,
maximum: 23
},
idleMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
store: { type: "string" },
typingIntervalSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
typingMode: { anyOf: [
{
type: "string",
const: "never"
},
{
type: "string",
const: "instant"
},
{
type: "string",
const: "thinking"
},
{
type: "string",
const: "message"
}
] },
parentForkMaxTokens: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
mainKey: { type: "string" },
sendPolicy: {
type: "object",
properties: {
default: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
rules: {
type: "array",
items: {
type: "object",
properties: {
action: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
match: {
type: "object",
properties: {
channel: { type: "string" },
chatType: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
keyPrefix: { type: "string" },
rawKeyPrefix: { type: "string" }
},
additionalProperties: false
}
},
required: ["action"],
additionalProperties: false
}
}
},
additionalProperties: false
},
agentToAgent: {
type: "object",
properties: { maxPingPongTurns: {
type: "integer",
minimum: 0,
maximum: 5
} },
additionalProperties: false
},
threadBindings: {
type: "object",
properties: {
enabled: { type: "boolean" },
idleHours: {
type: "number",
minimum: 0
},
maxAgeHours: {
type: "number",
minimum: 0
}
},
additionalProperties: false
},
maintenance: {
type: "object",
properties: {
mode: {
type: "string",
enum: ["enforce", "warn"]
},
pruneAfter: { anyOf: [{ type: "string" }, { type: "number" }] },
pruneDays: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxEntries: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
rotateBytes: { anyOf: [{ type: "string" }, { type: "number" }] },
resetArchiveRetention: { anyOf: [
{ type: "string" },
{ type: "number" },
{
type: "boolean",
const: false
}
] },
maxDiskBytes: { anyOf: [{ type: "string" }, { type: "number" }] },
highWaterBytes: { anyOf: [{ type: "string" }, { type: "number" }] }
},
additionalProperties: false
}
},
additionalProperties: false
},
cron: {
type: "object",
properties: {
enabled: { type: "boolean" },
store: { type: "string" },
maxConcurrentRuns: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
retry: {
type: "object",
properties: {
maxAttempts: {
type: "integer",
minimum: 0,
maximum: 10
},
backoffMs: {
minItems: 1,
maxItems: 10,
type: "array",
items: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
retryOn: {
minItems: 1,
type: "array",
items: {
type: "string",
enum: [
"rate_limit",
"overloaded",
"network",
"timeout",
"server_error"
]
}
}
},
additionalProperties: false
},
webhook: {
type: "string",
format: "uri"
},
webhookToken: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
sessionRetention: { anyOf: [{ type: "string" }, {
type: "boolean",
const: false
}] },
runLog: {
type: "object",
properties: {
maxBytes: { anyOf: [{ type: "string" }, { type: "number" }] },
keepLines: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
failureAlert: {
type: "object",
properties: {
enabled: { type: "boolean" },
after: {
type: "integer",
minimum: 1,
maximum: 9007199254740991
},
cooldownMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
mode: {
type: "string",
enum: ["announce", "webhook"]
},
accountId: { type: "string" }
},
additionalProperties: false
},
failureDestination: {
type: "object",
properties: {
channel: { type: "string" },
to: { type: "string" },
accountId: { type: "string" },
mode: {
type: "string",
enum: ["announce", "webhook"]
}
},
additionalProperties: false
}
},
additionalProperties: false
},
hooks: {
type: "object",
properties: {
enabled: { type: "boolean" },
path: { type: "string" },
token: { type: "string" },
defaultSessionKey: { type: "string" },
allowRequestSessionKey: { type: "boolean" },
allowedSessionKeyPrefixes: {
type: "array",
items: { type: "string" }
},
allowedAgentIds: {
type: "array",
items: { type: "string" }
},
maxBodyBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
presets: {
type: "array",
items: { type: "string" }
},
transformsDir: { type: "string" },
mappings: {
type: "array",
items: {
type: "object",
properties: {
id: { type: "string" },
match: {
type: "object",
properties: {
path: { type: "string" },
source: { type: "string" }
},
additionalProperties: false
},
action: { anyOf: [{
type: "string",
const: "wake"
}, {
type: "string",
const: "agent"
}] },
wakeMode: { anyOf: [{
type: "string",
const: "now"
}, {
type: "string",
const: "next-heartbeat"
}] },
name: { type: "string" },
agentId: { type: "string" },
sessionKey: { type: "string" },
messageTemplate: { type: "string" },
textTemplate: { type: "string" },
deliver: { type: "boolean" },
allowUnsafeExternalContent: { type: "boolean" },
channel: { anyOf: [
{
type: "string",
const: "last"
},
{
type: "string",
const: "whatsapp"
},
{
type: "string",
const: "telegram"
},
{
type: "string",
const: "discord"
},
{
type: "string",
const: "irc"
},
{
type: "string",
const: "slack"
},
{
type: "string",
const: "signal"
},
{
type: "string",
const: "imessage"
},
{
type: "string",
const: "msteams"
}
] },
to: { type: "string" },
model: { type: "string" },
thinking: { type: "string" },
timeoutSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
transform: {
type: "object",
properties: {
module: { type: "string" },
export: { type: "string" }
},
required: ["module"],
additionalProperties: false
}
},
additionalProperties: false
}
},
gmail: {
type: "object",
properties: {
account: { type: "string" },
label: { type: "string" },
topic: { type: "string" },
subscription: { type: "string" },
pushToken: { type: "string" },
hookUrl: { type: "string" },
includeBody: { type: "boolean" },
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
renewEveryMinutes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
allowUnsafeExternalContent: { type: "boolean" },
serve: {
type: "object",
properties: {
bind: { type: "string" },
port: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
path: { type: "string" }
},
additionalProperties: false
},
tailscale: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "serve"
},
{
type: "string",
const: "funnel"
}
] },
path: { type: "string" },
target: { type: "string" }
},
additionalProperties: false
},
model: { type: "string" },
thinking: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "minimal"
},
{
type: "string",
const: "low"
},
{
type: "string",
const: "medium"
},
{
type: "string",
const: "high"
}
] }
},
additionalProperties: false
},
internal: {
type: "object",
properties: {
enabled: { type: "boolean" },
handlers: {
type: "array",
items: {
type: "object",
properties: {
event: { type: "string" },
module: { type: "string" },
export: { type: "string" }
},
required: ["event", "module"],
additionalProperties: false
}
},
entries: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
enabled: { type: "boolean" },
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
}
},
additionalProperties: {}
}
},
load: {
type: "object",
properties: { extraDirs: {
type: "array",
items: { type: "string" }
} },
additionalProperties: false
},
installs: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
source: { anyOf: [
{
type: "string",
const: "npm"
},
{
type: "string",
const: "archive"
},
{
type: "string",
const: "path"
},
{
type: "string",
const: "clawhub"
}
] },
spec: { type: "string" },
sourcePath: { type: "string" },
installPath: { type: "string" },
version: { type: "string" },
resolvedName: { type: "string" },
resolvedVersion: { type: "string" },
resolvedSpec: { type: "string" },
integrity: { type: "string" },
shasum: { type: "string" },
resolvedAt: { type: "string" },
installedAt: { type: "string" },
clawhubUrl: { type: "string" },
clawhubPackage: { type: "string" },
clawhubFamily: { anyOf: [{
type: "string",
const: "code-plugin"
}, {
type: "string",
const: "bundle-plugin"
}] },
clawhubChannel: { anyOf: [
{
type: "string",
const: "official"
},
{
type: "string",
const: "community"
},
{
type: "string",
const: "private"
}
] },
hooks: {
type: "array",
items: { type: "string" }
}
},
required: ["source"],
additionalProperties: false
}
}
},
additionalProperties: false
}
},
additionalProperties: false
},
web: {
type: "object",
properties: {
enabled: { type: "boolean" },
heartbeatSeconds: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
reconnect: {
type: "object",
properties: {
initialMs: {
type: "number",
exclusiveMinimum: 0
},
maxMs: {
type: "number",
exclusiveMinimum: 0
},
factor: {
type: "number",
exclusiveMinimum: 0
},
jitter: {
type: "number",
minimum: 0,
maximum: 1
},
maxAttempts: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
channels: {
type: "object",
properties: {},
additionalProperties: true,
required: []
},
discovery: {
type: "object",
properties: {
wideArea: {
type: "object",
properties: {
enabled: { type: "boolean" },
domain: { type: "string" }
},
additionalProperties: false
},
mdns: {
type: "object",
properties: { mode: {
type: "string",
enum: [
"off",
"minimal",
"full"
]
} },
additionalProperties: false
}
},
additionalProperties: false
},
canvasHost: {
type: "object",
properties: {
enabled: { type: "boolean" },
root: { type: "string" },
port: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
liveReload: { type: "boolean" }
},
additionalProperties: false
},
talk: {
type: "object",
properties: {
provider: { type: "string" },
providers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
voiceId: { type: "string" },
voiceAliases: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
modelId: { type: "string" },
outputFormat: { type: "string" },
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] }
},
additionalProperties: {}
}
},
voiceId: { type: "string" },
voiceAliases: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
modelId: { type: "string" },
outputFormat: { type: "string" },
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
interruptOnSpeech: { type: "boolean" },
silenceTimeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
gateway: {
type: "object",
properties: {
port: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
mode: { anyOf: [{
type: "string",
const: "local"
}, {
type: "string",
const: "remote"
}] },
bind: { anyOf: [
{
type: "string",
const: "auto"
},
{
type: "string",
const: "lan"
},
{
type: "string",
const: "loopback"
},
{
type: "string",
const: "custom"
},
{
type: "string",
const: "tailnet"
}
] },
customBindHost: { type: "string" },
controlUi: {
type: "object",
properties: {
enabled: { type: "boolean" },
basePath: { type: "string" },
root: { type: "string" },
allowedOrigins: {
type: "array",
items: { type: "string" }
},
dangerouslyAllowHostHeaderOriginFallback: { type: "boolean" },
allowInsecureAuth: { type: "boolean" },
dangerouslyDisableDeviceAuth: { type: "boolean" }
},
additionalProperties: false
},
auth: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "none"
},
{
type: "string",
const: "token"
},
{
type: "string",
const: "password"
},
{
type: "string",
const: "trusted-proxy"
}
] },
token: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
password: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
allowTailscale: { type: "boolean" },
rateLimit: {
type: "object",
properties: {
maxAttempts: { type: "number" },
windowMs: { type: "number" },
lockoutMs: { type: "number" },
exemptLoopback: { type: "boolean" }
},
additionalProperties: false
},
trustedProxy: {
type: "object",
properties: {
userHeader: {
type: "string",
minLength: 1
},
requiredHeaders: {
type: "array",
items: { type: "string" }
},
allowUsers: {
type: "array",
items: { type: "string" }
}
},
required: ["userHeader"],
additionalProperties: false
}
},
additionalProperties: false
},
trustedProxies: {
type: "array",
items: { type: "string" }
},
allowRealIpFallback: { type: "boolean" },
tools: {
type: "object",
properties: {
deny: {
type: "array",
items: { type: "string" }
},
allow: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
channelHealthCheckMinutes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
channelStaleEventThresholdMinutes: {
type: "integer",
minimum: 1,
maximum: 9007199254740991
},
channelMaxRestartsPerHour: {
type: "integer",
minimum: 1,
maximum: 9007199254740991
},
tailscale: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "serve"
},
{
type: "string",
const: "funnel"
}
] },
resetOnExit: { type: "boolean" }
},
additionalProperties: false
},
remote: {
type: "object",
properties: {
url: { type: "string" },
transport: { anyOf: [{
type: "string",
const: "ssh"
}, {
type: "string",
const: "direct"
}] },
token: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
password: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
tlsFingerprint: { type: "string" },
sshTarget: { type: "string" },
sshIdentity: { type: "string" }
},
additionalProperties: false
},
reload: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "off"
},
{
type: "string",
const: "restart"
},
{
type: "string",
const: "hot"
},
{
type: "string",
const: "hybrid"
}
] },
debounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
deferralTimeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
tls: {
type: "object",
properties: {
enabled: { type: "boolean" },
autoGenerate: { type: "boolean" },
certPath: { type: "string" },
keyPath: { type: "string" },
caPath: { type: "string" }
},
additionalProperties: false
},
http: {
type: "object",
properties: {
endpoints: {
type: "object",
properties: {
chatCompletions: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxBodyBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxImageParts: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxTotalImageBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
images: {
type: "object",
properties: {
allowUrl: { type: "boolean" },
urlAllowlist: {
type: "array",
items: { type: "string" }
},
allowedMimes: {
type: "array",
items: { type: "string" }
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxRedirects: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
timeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
responses: {
type: "object",
properties: {
enabled: { type: "boolean" },
maxBodyBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxUrlParts: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
files: {
type: "object",
properties: {
allowUrl: { type: "boolean" },
urlAllowlist: {
type: "array",
items: { type: "string" }
},
allowedMimes: {
type: "array",
items: { type: "string" }
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxRedirects: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
timeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
pdf: {
type: "object",
properties: {
maxPages: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxPixels: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
minTextChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
},
images: {
type: "object",
properties: {
allowUrl: { type: "boolean" },
urlAllowlist: {
type: "array",
items: { type: "string" }
},
allowedMimes: {
type: "array",
items: { type: "string" }
},
maxBytes: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxRedirects: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
timeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
},
securityHeaders: {
type: "object",
properties: { strictTransportSecurity: { anyOf: [{ type: "string" }, {
type: "boolean",
const: false
}] } },
additionalProperties: false
}
},
additionalProperties: false
},
push: {
type: "object",
properties: { apns: {
type: "object",
properties: { relay: {
type: "object",
properties: {
baseUrl: { type: "string" },
timeoutMs: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
} },
additionalProperties: false
} },
additionalProperties: false
},
nodes: {
type: "object",
properties: {
browser: {
type: "object",
properties: {
mode: { anyOf: [
{
type: "string",
const: "auto"
},
{
type: "string",
const: "manual"
},
{
type: "string",
const: "off"
}
] },
node: { type: "string" }
},
additionalProperties: false
},
allowCommands: {
type: "array",
items: { type: "string" }
},
denyCommands: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
}
},
additionalProperties: false
},
memory: {
type: "object",
properties: {
backend: { anyOf: [{
type: "string",
const: "builtin"
}, {
type: "string",
const: "qmd"
}] },
citations: { anyOf: [
{
type: "string",
const: "auto"
},
{
type: "string",
const: "on"
},
{
type: "string",
const: "off"
}
] },
qmd: {
type: "object",
properties: {
command: { type: "string" },
mcporter: {
type: "object",
properties: {
enabled: { type: "boolean" },
serverName: { type: "string" },
startDaemon: { type: "boolean" }
},
additionalProperties: false
},
searchMode: { anyOf: [
{
type: "string",
const: "query"
},
{
type: "string",
const: "search"
},
{
type: "string",
const: "vsearch"
}
] },
includeDefaultMemory: { type: "boolean" },
paths: {
type: "array",
items: {
type: "object",
properties: {
path: { type: "string" },
name: { type: "string" },
pattern: { type: "string" }
},
required: ["path"],
additionalProperties: false
}
},
sessions: {
type: "object",
properties: {
enabled: { type: "boolean" },
exportDir: { type: "string" },
retentionDays: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
update: {
type: "object",
properties: {
interval: { type: "string" },
debounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
onBoot: { type: "boolean" },
waitForBootSync: { type: "boolean" },
embedInterval: { type: "string" },
commandTimeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
updateTimeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
embedTimeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
limits: {
type: "object",
properties: {
maxResults: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxSnippetChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
maxInjectedChars: {
type: "integer",
exclusiveMinimum: 0,
maximum: 9007199254740991
},
timeoutMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
scope: {
type: "object",
properties: {
default: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
rules: {
type: "array",
items: {
type: "object",
properties: {
action: { anyOf: [{
type: "string",
const: "allow"
}, {
type: "string",
const: "deny"
}] },
match: {
type: "object",
properties: {
channel: { type: "string" },
chatType: { anyOf: [
{
type: "string",
const: "direct"
},
{
type: "string",
const: "group"
},
{
type: "string",
const: "channel"
},
{
type: "string",
const: "dm"
}
] },
keyPrefix: { type: "string" },
rawKeyPrefix: { type: "string" }
},
additionalProperties: false
}
},
required: ["action"],
additionalProperties: false
}
}
},
additionalProperties: false
}
},
additionalProperties: false
}
},
additionalProperties: false
},
mcp: {
type: "object",
properties: { servers: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
command: { type: "string" },
args: {
type: "array",
items: { type: "string" }
},
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { anyOf: [
{ type: "string" },
{ type: "number" },
{ type: "boolean" }
] }
},
cwd: { type: "string" },
workingDirectory: { type: "string" },
url: {
type: "string",
format: "uri"
}
},
additionalProperties: {}
}
} },
additionalProperties: false
},
skills: {
type: "object",
properties: {
allowBundled: {
type: "array",
items: { type: "string" }
},
load: {
type: "object",
properties: {
extraDirs: {
type: "array",
items: { type: "string" }
},
watch: { type: "boolean" },
watchDebounceMs: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
install: {
type: "object",
properties: {
preferBrew: { type: "boolean" },
nodeManager: { anyOf: [
{
type: "string",
const: "npm"
},
{
type: "string",
const: "pnpm"
},
{
type: "string",
const: "yarn"
},
{
type: "string",
const: "bun"
}
] }
},
additionalProperties: false
},
limits: {
type: "object",
properties: {
maxCandidatesPerRoot: {
type: "integer",
minimum: 1,
maximum: 9007199254740991
},
maxSkillsLoadedPerSource: {
type: "integer",
minimum: 1,
maximum: 9007199254740991
},
maxSkillsInPrompt: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxSkillsPromptChars: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
},
maxSkillFileBytes: {
type: "integer",
minimum: 0,
maximum: 9007199254740991
}
},
additionalProperties: false
},
entries: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
enabled: { type: "boolean" },
apiKey: { anyOf: [{ type: "string" }, { oneOf: [
{
type: "object",
properties: {
source: {
type: "string",
const: "env"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: {
type: "string",
pattern: "^[A-Z][A-Z0-9_]{0,127}$"
}
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "file"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
},
{
type: "object",
properties: {
source: {
type: "string",
const: "exec"
},
provider: {
type: "string",
pattern: "^[a-z][a-z0-9_-]{0,63}$"
},
id: { type: "string" }
},
required: [
"source",
"provider",
"id"
],
additionalProperties: false
}
] }] },
env: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: { type: "string" }
},
config: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {}
}
},
additionalProperties: false
}
}
},
additionalProperties: false
},
plugins: {
type: "object",
properties: {
enabled: { type: "boolean" },
allow: {
type: "array",
items: { type: "string" }
},
deny: {
type: "array",
items: { type: "string" }
},
load: {
type: "object",
properties: { paths: {
type: "array",
items: { type: "string" }
} },
additionalProperties: false
},
slots: {
type: "object",
properties: {
memory: { type: "string" },
contextEngine: { type: "string" }
},
additionalProperties: false
},
entries: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
enabled: { type: "boolean" },
hooks: {
type: "object",
properties: { allowPromptInjection: { type: "boolean" } },
additionalProperties: false
},
subagent: {
type: "object",
properties: {
allowModelOverride: { type: "boolean" },
allowedModels: {
type: "array",
items: { type: "string" }
}
},
additionalProperties: false
},
config: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {}
}
},
additionalProperties: false
}
},
installs: {
type: "object",
propertyNames: { type: "string" },
additionalProperties: {
type: "object",
properties: {
source: { anyOf: [{ anyOf: [
{
type: "string",
const: "npm"
},
{
type: "string",
const: "archive"
},
{
type: "string",
const: "path"
},
{
type: "string",
const: "clawhub"
}
] }, {
type: "string",
const: "marketplace"
}] },
spec: { type: "string" },
sourcePath: { type: "string" },
installPath: { type: "string" },
version: { type: "string" },
resolvedName: { type: "string" },
resolvedVersion: { type: "string" },
resolvedSpec: { type: "string" },
integrity: { type: "string" },
shasum: { type: "string" },
resolvedAt: { type: "string" },
installedAt: { type: "string" },
clawhubUrl: { type: "string" },
clawhubPackage: { type: "string" },
clawhubFamily: { anyOf: [{
type: "string",
const: "code-plugin"
}, {
type: "string",
const: "bundle-plugin"
}] },
clawhubChannel: { anyOf: [
{
type: "string",
const: "official"
},
{
type: "string",
const: "community"
},
{
type: "string",
const: "private"
}
] },
marketplaceName: { type: "string" },
marketplaceSource: { type: "string" },
marketplacePlugin: { type: "string" }
},
required: ["source"],
additionalProperties: false
}
}
},
additionalProperties: false
}
},
required: ["commands"],
additionalProperties: false,
title: "OpenClawConfig"
},
uiHints: {
wizard: {
label: "Setup Wizard State",
group: "Wizard",
order: 20,
help: "Setup wizard state tracking fields that record the most recent guided setup run details. Keep these fields for observability and troubleshooting of setup flows across upgrades.",
tags: ["advanced"]
},
update: {
label: "Updates",
group: "Update",
order: 25,
help: "Update-channel and startup-check behavior for keeping OpenClaw runtime versions current. Use conservative channels in production and more experimental channels only in controlled environments.",
tags: ["advanced"]
},
cli: {
label: "CLI",
group: "CLI",
order: 26,
help: "CLI presentation controls for local command output behavior such as banner and tagline style. Use this section to keep startup output aligned with operator preference without changing runtime behavior.",
tags: ["advanced"]
},
diagnostics: {
label: "Diagnostics",
group: "Diagnostics",
order: 27,
help: "Diagnostics controls for targeted tracing, telemetry export, and cache inspection during debugging. Keep baseline diagnostics minimal in production and enable deeper signals only when investigating issues.",
tags: ["observability"]
},
logging: {
label: "Logging",
group: "Logging",
order: 900,
help: "Logging behavior controls for severity, output destinations, formatting, and sensitive-data redaction. Keep levels and redaction strict enough for production while preserving useful diagnostics.",
tags: ["advanced"]
},
gateway: {
label: "Gateway",
group: "Gateway",
order: 30,
help: "Gateway runtime surface for bind mode, auth, control UI, remote transport, and operational safety controls. Keep conservative defaults unless you intentionally expose the gateway beyond trusted local interfaces.",
tags: ["advanced"]
},
nodeHost: {
label: "Node Host",
group: "Node Host",
order: 35,
help: "Node host controls for features exposed from this gateway node to other nodes or clients. Keep defaults unless you intentionally proxy local capabilities across your node network.",
tags: ["advanced"]
},
agents: {
label: "Agents",
group: "Agents",
order: 40,
help: "Agent runtime configuration root covering defaults and explicit agent entries used for routing and execution context. Keep this section explicit so model/tool behavior stays predictable across multi-agent workflows.",
tags: ["advanced"]
},
tools: {
label: "Tools",
group: "Tools",
order: 50,
help: "Global tool access policy and capability configuration across web, exec, media, messaging, and elevated surfaces. Use this section to constrain risky capabilities before broad rollout.",
tags: ["advanced"]
},
bindings: {
label: "Bindings",
group: "Bindings",
order: 55,
help: "Top-level binding rules for routing and persistent ACP conversation ownership. Use type=route for normal routing and type=acp for persistent ACP harness bindings.",
tags: ["advanced"]
},
audio: {
label: "Audio",
group: "Audio",
order: 60,
help: "Global audio ingestion settings used before higher-level tools process speech or media content. Configure this when you need deterministic transcription behavior for voice notes and clips.",
tags: ["advanced"]
},
models: {
label: "Models",
group: "Models",
order: 70,
help: "Model catalog root for provider definitions, merge/replace behavior, and optional Bedrock discovery integration. Keep provider definitions explicit and validated before relying on production failover paths.",
tags: ["models"]
},
messages: {
label: "Messages",
group: "Messages",
order: 80,
help: "Message formatting, acknowledgment, queueing, debounce, and status reaction behavior for inbound/outbound chat flows. Use this section when channel responsiveness or message UX needs adjustment.",
tags: ["advanced"]
},
commands: {
label: "Commands",
group: "Commands",
order: 85,
help: "Controls chat command surfaces, owner gating, and elevated command access behavior across providers. Keep defaults unless you need stricter operator controls or broader command availability.",
tags: ["advanced"]
},
session: {
label: "Session",
group: "Session",
order: 90,
help: "Global session routing, reset, delivery policy, and maintenance controls for conversation history behavior. Keep defaults unless you need stricter isolation, retention, or delivery constraints.",
tags: ["storage"]
},
cron: {
label: "Cron",
group: "Cron",
order: 100,
help: "Global scheduler settings for stored cron jobs, run concurrency, delivery fallback, and run-session retention. Keep defaults unless you are scaling job volume or integrating external webhook receivers.",
tags: ["automation"]
},
hooks: {
label: "Hooks",
group: "Hooks",
order: 110,
help: "Inbound webhook automation surface for mapping external events into wake or agent actions in OpenClaw. Keep this locked down with explicit token/session/agent controls before exposing it beyond trusted networks.",
tags: ["advanced"]
},
ui: {
label: "UI",
group: "UI",
order: 120,
help: "UI presentation settings for accenting and assistant identity shown in control surfaces. Use this for branding and readability customization without changing runtime behavior.",
tags: ["advanced"]
},
browser: {
label: "Browser",
group: "Browser",
order: 130,
help: "Browser runtime controls for local or remote CDP attachment, profile routing, and screenshot/snapshot behavior. Keep defaults unless your automation workflow requires custom browser transport settings.",
tags: ["advanced"]
},
talk: {
label: "Talk",
group: "Talk",
order: 140,
help: "Talk-mode voice synthesis settings for voice identity, model selection, output format, and interruption behavior. Use this section to tune human-facing voice UX while controlling latency and cost.",
tags: ["advanced"]
},
channels: {
label: "Channels",
group: "Messaging Channels",
order: 150,
help: "Channel provider configurations plus shared defaults that control access policies, heartbeat visibility, and per-surface behavior. Keep defaults centralized and override per provider only where required.",
tags: ["advanced"]
},
skills: {
label: "Skills",
group: "Skills",
order: 200,
tags: ["advanced"]
},
plugins: {
label: "Plugins",
group: "Plugins",
order: 205,
help: "Plugin system controls for enabling extensions, constraining load scope, configuring entries, and tracking installs. Keep plugin policy explicit and least-privilege in production environments.",
tags: ["advanced"]
},
discovery: {
label: "Discovery",
group: "Discovery",
order: 210,
help: "Service discovery settings for local mDNS advertisement and optional wide-area presence signaling. Keep discovery scoped to expected networks to avoid leaking service metadata.",
tags: ["advanced"]
},
presence: {
label: "Presence",
group: "Presence",
order: 220,
tags: ["advanced"]
},
voicewake: {
label: "Voice Wake",
group: "Voice Wake",
order: 230,
tags: ["advanced"]
},
meta: {
label: "Metadata",
help: "Metadata fields automatically maintained by OpenClaw to record write/version history for this config file. Keep these values system-managed and avoid manual edits unless debugging migration history.",
tags: ["advanced"]
},
"meta.lastTouchedVersion": {
label: "Config Last Touched Version",
help: "Auto-set when OpenClaw writes the config.",
tags: ["media"]
},
"meta.lastTouchedAt": {
label: "Config Last Touched At",
help: "ISO timestamp of the last config write (auto-set).",
tags: ["media"]
},
env: {
label: "Environment",
help: "Environment import and override settings used to supply runtime variables to the gateway process. Use this section to control shell-env loading and explicit variable injection behavior.",
tags: ["advanced"]
},
"env.shellEnv": {
label: "Shell Environment Import",
help: "Shell environment import controls for loading variables from your login shell during startup. Keep this enabled when you depend on profile-defined secrets or PATH customizations.",
tags: ["advanced"]
},
"env.shellEnv.enabled": {
label: "Shell Environment Import Enabled",
help: "Enables loading environment variables from the user shell profile during startup initialization. Keep enabled for developer machines, or disable in locked-down service environments with explicit env management.",
tags: ["advanced"]
},
"env.shellEnv.timeoutMs": {
label: "Shell Environment Import Timeout (ms)",
help: "Maximum time in milliseconds allowed for shell environment resolution before fallback behavior applies. Use tighter timeouts for faster startup, or increase when shell initialization is heavy.",
tags: ["performance"]
},
"env.vars": {
label: "Environment Variable Overrides",
help: "Explicit key/value environment variable overrides merged into runtime process environment for OpenClaw. Use this for deterministic env configuration instead of relying only on shell profile side effects.",
tags: ["advanced"]
},
"wizard.lastRunAt": {
label: "Wizard Last Run Timestamp",
help: "ISO timestamp for when the setup wizard most recently completed on this host. Use this to confirm setup recency during support and operational audits.",
tags: ["advanced"]
},
"wizard.lastRunVersion": {
label: "Wizard Last Run Version",
help: "OpenClaw version recorded at the time of the most recent wizard run on this config. Use this when diagnosing behavior differences across version-to-version setup changes.",
tags: ["advanced"]
},
"wizard.lastRunCommit": {
label: "Wizard Last Run Commit",
help: "Source commit identifier recorded for the last wizard execution in development builds. Use this to correlate setup behavior with exact source state during debugging.",
tags: ["advanced"]
},
"wizard.lastRunCommand": {
label: "Wizard Last Run Command",
help: "Command invocation recorded for the latest wizard run to preserve execution context. Use this to reproduce setup steps when verifying setup regressions.",
tags: ["advanced"]
},
"wizard.lastRunMode": {
label: "Wizard Last Run Mode",
help: "Wizard execution mode recorded as \"local\" or \"remote\" for the most recent setup flow. Use this to understand whether setup targeted direct local runtime or remote gateway topology.",
tags: ["advanced"]
},
"diagnostics.otel": {
label: "OpenTelemetry",
help: "OpenTelemetry export settings for traces, metrics, and logs emitted by gateway components. Use this when integrating with centralized observability backends and distributed tracing pipelines.",
tags: ["observability"]
},
"diagnostics.cacheTrace": {
label: "Cache Trace",
help: "Cache-trace logging settings for observing cache decisions and payload context in embedded runs. Enable this temporarily for debugging and disable afterward to reduce sensitive log footprint.",
tags: ["observability", "storage"]
},
"logging.level": {
label: "Log Level",
help: "Primary log level threshold for runtime logger output: \"silent\", \"fatal\", \"error\", \"warn\", \"info\", \"debug\", or \"trace\". Keep \"info\" or \"warn\" for production, and use debug/trace only during investigation.",
tags: ["observability"]
},
"logging.file": {
label: "Log File Path",
help: "Optional file path for persisted log output in addition to or instead of console logging. Use a managed writable path and align retention/rotation with your operational policy.",
tags: ["observability", "storage"]
},
"logging.consoleLevel": {
label: "Console Log Level",
help: "Console-specific log threshold: \"silent\", \"fatal\", \"error\", \"warn\", \"info\", \"debug\", or \"trace\" for terminal output control. Use this to keep local console quieter while retaining richer file logging if needed.",
tags: ["observability"]
},
"logging.consoleStyle": {
label: "Console Log Style",
help: "Console output format style: \"pretty\", \"compact\", or \"json\" based on operator and ingestion needs. Use json for machine parsing pipelines and pretty/compact for human-first terminal workflows.",
tags: ["observability"]
},
"logging.redactSensitive": {
label: "Sensitive Data Redaction Mode",
help: "Sensitive redaction mode: \"off\" disables built-in masking, while \"tools\" redacts sensitive tool/config payload fields. Keep \"tools\" in shared logs unless you have isolated secure log sinks.",
tags: ["privacy", "observability"]
},
"logging.redactPatterns": {
label: "Custom Redaction Patterns",
help: "Additional custom redact regex patterns applied to log output before emission/storage. Use this to mask org-specific tokens and identifiers not covered by built-in redaction rules.",
tags: ["privacy", "observability"]
},
"cli.banner": {
label: "CLI Banner",
help: "CLI startup banner controls for title/version line and tagline style behavior. Keep banner enabled for fast version/context checks, then tune tagline mode to your preferred noise level.",
tags: ["advanced"]
},
"cli.banner.taglineMode": {
label: "CLI Banner Tagline Mode",
help: "Controls tagline style in the CLI startup banner: \"random\" (default) picks from the rotating tagline pool, \"default\" always shows the neutral default tagline, and \"off\" hides tagline text while keeping the banner version line.",
tags: ["advanced"]
},
"update.channel": {
label: "Update Channel",
help: "Update channel for git + npm installs (\"stable\", \"beta\", or \"dev\").",
tags: ["advanced"]
},
"update.checkOnStart": {
label: "Update Check on Start",
help: "Check for npm updates when the gateway starts (default: true).",
tags: ["automation"]
},
"update.auto.enabled": {
label: "Auto Update Enabled",
help: "Enable background auto-update for package installs (default: false).",
tags: ["advanced"]
},
"update.auto.stableDelayHours": {
label: "Auto Update Stable Delay (hours)",
help: "Minimum delay before stable-channel auto-apply starts (default: 6).",
tags: ["advanced"]
},
"update.auto.stableJitterHours": {
label: "Auto Update Stable Jitter (hours)",
help: "Extra stable-channel rollout spread window in hours (default: 12).",
tags: ["advanced"]
},
"update.auto.betaCheckIntervalHours": {
label: "Auto Update Beta Check Interval (hours)",
help: "How often beta-channel checks run in hours (default: 1).",
tags: ["performance"]
},
"diagnostics.enabled": {
label: "Diagnostics Enabled",
help: "Master toggle for diagnostics instrumentation output in logs and telemetry wiring paths. Keep enabled for normal observability, and disable only in tightly constrained environments.",
tags: ["observability"]
},
"diagnostics.flags": {
label: "Diagnostics Flags",
help: "Enable targeted diagnostics logs by flag (e.g. [\"telegram.http\"]). Supports wildcards like \"telegram.*\" or \"*\".",
tags: ["observability"]
},
"diagnostics.stuckSessionWarnMs": {
label: "Stuck Session Warning Threshold (ms)",
help: "Age threshold in milliseconds for emitting stuck-session warnings while a session remains in processing state. Increase for long multi-tool turns to reduce false positives; decrease for faster hang detection.",
tags: ["observability", "storage"]
},
"diagnostics.otel.enabled": {
label: "OpenTelemetry Enabled",
help: "Enables OpenTelemetry export pipeline for traces, metrics, and logs based on configured endpoint/protocol settings. Keep disabled unless your collector endpoint and auth are fully configured.",
tags: ["observability"]
},
"diagnostics.otel.endpoint": {
label: "OpenTelemetry Endpoint",
help: "Collector endpoint URL used for OpenTelemetry export transport, including scheme and port. Use a reachable, trusted collector endpoint and monitor ingestion errors after rollout.",
tags: ["observability"]
},
"diagnostics.otel.protocol": {
label: "OpenTelemetry Protocol",
help: "OTel transport protocol for telemetry export: \"http/protobuf\" or \"grpc\" depending on collector support. Use the protocol your observability backend expects to avoid dropped telemetry payloads.",
tags: ["observability"]
},
"diagnostics.otel.headers": {
label: "OpenTelemetry Headers",
help: "Additional HTTP/gRPC metadata headers sent with OpenTelemetry export requests, often used for tenant auth or routing. Keep secrets in env-backed values and avoid unnecessary header sprawl.",
tags: ["observability"]
},
"diagnostics.otel.serviceName": {
label: "OpenTelemetry Service Name",
help: "Service name reported in telemetry resource attributes to identify this gateway instance in observability backends. Use stable names so dashboards and alerts remain consistent over deployments.",
tags: ["observability"]
},
"diagnostics.otel.traces": {
label: "OpenTelemetry Traces Enabled",
help: "Enable trace signal export to the configured OpenTelemetry collector endpoint. Keep enabled when latency/debug tracing is needed, and disable if you only want metrics/logs.",
tags: ["observability"]
},
"diagnostics.otel.metrics": {
label: "OpenTelemetry Metrics Enabled",
help: "Enable metrics signal export to the configured OpenTelemetry collector endpoint. Keep enabled for runtime health dashboards, and disable only if metric volume must be minimized.",
tags: ["observability"]
},
"diagnostics.otel.logs": {
label: "OpenTelemetry Logs Enabled",
help: "Enable log signal export through OpenTelemetry in addition to local logging sinks. Use this when centralized log correlation is required across services and agents.",
tags: ["observability"]
},
"diagnostics.otel.sampleRate": {
label: "OpenTelemetry Trace Sample Rate",
help: "Trace sampling rate (0-1) controlling how much trace traffic is exported to observability backends. Lower rates reduce overhead/cost, while higher rates improve debugging fidelity.",
tags: ["observability"]
},
"diagnostics.otel.flushIntervalMs": {
label: "OpenTelemetry Flush Interval (ms)",
help: "Interval in milliseconds for periodic telemetry flush from buffers to the collector. Increase to reduce export chatter, or lower for faster visibility during active incident response.",
tags: ["observability", "performance"]
},
"diagnostics.cacheTrace.enabled": {
label: "Cache Trace Enabled",
help: "Log cache trace snapshots for embedded agent runs (default: false).",
tags: ["observability", "storage"]
},
"diagnostics.cacheTrace.filePath": {
label: "Cache Trace File Path",
help: "JSONL output path for cache trace logs (default: $OPENCLAW_STATE_DIR/logs/cache-trace.jsonl).",
tags: ["observability", "storage"]
},
"diagnostics.cacheTrace.includeMessages": {
label: "Cache Trace Include Messages",
help: "Include full message payloads in trace output (default: true).",
tags: ["observability", "storage"]
},
"diagnostics.cacheTrace.includePrompt": {
label: "Cache Trace Include Prompt",
help: "Include prompt text in trace output (default: true).",
tags: ["observability", "storage"]
},
"diagnostics.cacheTrace.includeSystem": {
label: "Cache Trace Include System",
help: "Include system prompt in trace output (default: true).",
tags: ["observability", "storage"]
},
"agents.list.*.identity.avatar": {
label: "Identity Avatar",
help: "Agent avatar (workspace-relative path, http(s) URL, or data URI).",
tags: ["advanced"]
},
"agents.list.*.skills": {
label: "Agent Skill Filter",
help: "Optional allowlist of skills for this agent (omit = all skills; empty = no skills).",
tags: ["advanced"]
},
"agents.list[].runtime": {
label: "Agent Runtime",
help: "Optional runtime descriptor for this agent. Use embedded for default OpenClaw execution or acp for external ACP harness defaults.",
tags: ["advanced"]
},
"agents.list[].runtime.type": {
label: "Agent Runtime Type",
help: "Runtime type for this agent: \"embedded\" (default OpenClaw runtime) or \"acp\" (ACP harness defaults).",
tags: ["advanced"]
},
"agents.list[].runtime.acp": {
label: "Agent ACP Runtime",
help: "ACP runtime defaults for this agent when runtime.type=acp. Binding-level ACP overrides still take precedence per conversation.",
tags: ["advanced"]
},
"agents.list[].runtime.acp.agent": {
label: "Agent ACP Harness Agent",
help: "Optional ACP harness agent id to use for this OpenClaw agent (for example codex, claude).",
tags: ["advanced"]
},
"agents.list[].runtime.acp.backend": {
label: "Agent ACP Backend",
help: "Optional ACP backend override for this agent's ACP sessions (falls back to global acp.backend).",
tags: ["advanced"]
},
"agents.list[].runtime.acp.mode": {
label: "Agent ACP Mode",
help: "Optional ACP session mode default for this agent (persistent or oneshot).",
tags: ["advanced"]
},
"agents.list[].runtime.acp.cwd": {
label: "Agent ACP Working Directory",
help: "Optional default working directory for this agent's ACP sessions.",
tags: ["advanced"]
},
"agents.list[].thinkingDefault": {
label: "Agent Thinking Default",
help: "Optional per-agent default thinking level. Overrides agents.defaults.thinkingDefault for this agent when no per-message or session override is set.",
tags: ["advanced"]
},
"agents.list[].reasoningDefault": {
label: "Agent Reasoning Default",
help: "Optional per-agent default reasoning visibility (on|off|stream). Applies when no per-message or session reasoning override is set.",
tags: ["advanced"]
},
"agents.list[].fastModeDefault": {
label: "Agent Fast Mode Default",
help: "Optional per-agent default for fast mode. Applies when no per-message or session fast-mode override is set.",
tags: ["advanced"]
},
"agents.defaults": {
label: "Agent Defaults",
help: "Shared default settings inherited by agents unless overridden per entry in agents.list. Use defaults to enforce consistent baseline behavior and reduce duplicated per-agent configuration.",
tags: ["advanced"]
},
"agents.list": {
label: "Agent List",
help: "Explicit list of configured agents with IDs and optional overrides for model, tools, identity, and workspace. Keep IDs stable over time so bindings, approvals, and session routing remain deterministic.",
tags: ["advanced"]
},
"gateway.port": {
label: "Gateway Port",
help: "TCP port used by the gateway listener for API, control UI, and channel-facing ingress paths. Use a dedicated port and avoid collisions with reverse proxies or local developer services.",
tags: ["network"]
},
"gateway.mode": {
label: "Gateway Mode",
help: "Gateway operation mode: \"local\" runs channels and agent runtime on this host, while \"remote\" connects through remote transport. Keep \"local\" unless you intentionally run a split remote gateway topology.",
tags: ["network"]
},
"gateway.bind": {
label: "Gateway Bind Mode",
help: "Network bind profile: \"auto\", \"lan\", \"loopback\", \"custom\", or \"tailnet\" to control interface exposure. Keep \"loopback\" or \"auto\" for safest local operation unless external clients must connect.",
tags: ["network"]
},
"gateway.customBindHost": {
label: "Gateway Custom Bind Host",
help: "Explicit bind host/IP used when gateway.bind is set to custom for manual interface targeting. Use a precise address and avoid wildcard binds unless external exposure is required.",
tags: ["network"]
},
"gateway.controlUi": {
label: "Control UI",
help: "Control UI hosting settings including enablement, pathing, and browser-origin/auth hardening behavior. Keep UI exposure minimal and pair with strong auth controls before internet-facing deployments.",
tags: ["network"]
},
"gateway.controlUi.enabled": {
label: "Control UI Enabled",
help: "Enables serving the gateway Control UI from the gateway HTTP process when true. Keep enabled for local administration, and disable when an external control surface replaces it.",
tags: ["network"]
},
"gateway.auth": {
label: "Gateway Auth",
help: "Authentication policy for gateway HTTP/WebSocket access including mode, credentials, trusted-proxy behavior, and rate limiting. Keep auth enabled for every non-loopback deployment.",
tags: ["network"]
},
"gateway.auth.mode": {
label: "Gateway Auth Mode",
help: "Gateway auth mode: \"none\", \"token\", \"password\", or \"trusted-proxy\" depending on your edge architecture. Use token/password for direct exposure, and trusted-proxy only behind hardened identity-aware proxies.",
tags: ["network"]
},
"gateway.auth.allowTailscale": {
label: "Gateway Auth Allow Tailscale Identity",
help: "Allows trusted Tailscale identity paths to satisfy gateway auth checks when configured. Use this only when your tailnet identity posture is strong and operator workflows depend on it.",
tags: ["access", "network"]
},
"gateway.auth.rateLimit": {
label: "Gateway Auth Rate Limit",
help: "Login/auth attempt throttling controls to reduce credential brute-force risk at the gateway boundary. Keep enabled in exposed environments and tune thresholds to your traffic baseline.",
tags: ["network", "performance"]
},
"gateway.auth.trustedProxy": {
label: "Gateway Trusted Proxy Auth",
help: "Trusted-proxy auth header mapping for upstream identity providers that inject user claims. Use only with known proxy CIDRs and strict header allowlists to prevent spoofed identity headers.",
tags: ["network"]
},
"gateway.trustedProxies": {
label: "Gateway Trusted Proxy CIDRs",
help: "CIDR/IP allowlist of upstream proxies permitted to provide forwarded client identity headers. Keep this list narrow so untrusted hops cannot impersonate users.",
tags: ["network"]
},
"gateway.allowRealIpFallback": {
label: "Gateway Allow x-real-ip Fallback",
help: "Enables x-real-ip fallback when x-forwarded-for is missing in proxy scenarios. Keep disabled unless your ingress stack requires this compatibility behavior.",
tags: [
"access",
"network",
"reliability"
]
},
"gateway.tools": {
label: "Gateway Tool Exposure Policy",
help: "Gateway-level tool exposure allow/deny policy that can restrict runtime tool availability independent of agent/tool profiles. Use this for coarse emergency controls and production hardening.",
tags: ["network"]
},
"gateway.tools.allow": {
label: "Gateway Tool Allowlist",
help: "Explicit gateway-level tool allowlist when you want a narrow set of tools available at runtime. Use this for locked-down environments where tool scope must be tightly controlled.",
tags: ["access", "network"]
},
"gateway.tools.deny": {
label: "Gateway Tool Denylist",
help: "Explicit gateway-level tool denylist to block risky tools even if lower-level policies allow them. Use deny rules for emergency response and defense-in-depth hardening.",
tags: ["access", "network"]
},
"gateway.channelHealthCheckMinutes": {
label: "Gateway Channel Health Check Interval (min)",
help: "Interval in minutes for automatic channel health probing and status updates. Use lower intervals for faster detection, or higher intervals to reduce periodic probe noise.",
tags: ["network", "reliability"]
},
"gateway.channelStaleEventThresholdMinutes": {
label: "Gateway Channel Stale Event Threshold (min)",
help: "How many minutes a connected channel can go without receiving any event before the health monitor treats it as a stale socket and triggers a restart. Default: 30.",
tags: ["network"]
},
"gateway.channelMaxRestartsPerHour": {
label: "Gateway Channel Max Restarts Per Hour",
help: "Maximum number of health-monitor-initiated channel restarts allowed within a rolling one-hour window. Once hit, further restarts are skipped until the window expires. Default: 10.",
tags: ["network", "performance"]
},
"gateway.tailscale": {
label: "Gateway Tailscale",
help: "Tailscale integration settings for Serve/Funnel exposure and lifecycle handling on gateway start/exit. Keep off unless your deployment intentionally relies on Tailscale ingress.",
tags: ["network"]
},
"gateway.tailscale.mode": {
label: "Gateway Tailscale Mode",
help: "Tailscale publish mode: \"off\", \"serve\", or \"funnel\" for private or public exposure paths. Use \"serve\" for tailnet-only access and \"funnel\" only when public internet reachability is required.",
tags: ["network"]
},
"gateway.tailscale.resetOnExit": {
label: "Gateway Tailscale Reset on Exit",
help: "Resets Tailscale Serve/Funnel state on gateway exit to avoid stale published routes after shutdown. Keep enabled unless another controller manages publish lifecycle outside the gateway.",
tags: ["network"]
},
"gateway.remote": {
label: "Remote Gateway",
help: "Remote gateway connection settings for direct or SSH transport when this instance proxies to another runtime host. Use remote mode only when split-host operation is intentionally configured.",
tags: ["network"]
},
"gateway.remote.transport": {
label: "Remote Gateway Transport",
help: "Remote connection transport: \"direct\" uses configured URL connectivity, while \"ssh\" tunnels through SSH. Use SSH when you need encrypted tunnel semantics without exposing remote ports.",
tags: ["network"]
},
"gateway.reload": {
label: "Config Reload",
help: "Live config-reload policy for how edits are applied and when full restarts are triggered. Keep hybrid behavior for safest operational updates unless debugging reload internals.",
tags: ["network", "reliability"]
},
"gateway.tls": {
label: "Gateway TLS",
help: "TLS certificate and key settings for terminating HTTPS directly in the gateway process. Use explicit certificates in production and avoid plaintext exposure on untrusted networks.",
tags: ["network"]
},
"gateway.tls.enabled": {
label: "Gateway TLS Enabled",
help: "Enables TLS termination at the gateway listener so clients connect over HTTPS/WSS directly. Keep enabled for direct internet exposure or any untrusted network boundary.",
tags: ["network"]
},
"gateway.tls.autoGenerate": {
label: "Gateway TLS Auto-Generate Cert",
help: "Auto-generates a local TLS certificate/key pair when explicit files are not configured. Use only for local/dev setups and replace with real certificates for production traffic.",
tags: ["network"]
},
"gateway.tls.certPath": {
label: "Gateway TLS Certificate Path",
help: "Filesystem path to the TLS certificate file used by the gateway when TLS is enabled. Use managed certificate paths and keep renewal automation aligned with this location.",
tags: ["network", "storage"]
},
"gateway.tls.keyPath": {
label: "Gateway TLS Key Path",
help: "Filesystem path to the TLS private key file used by the gateway when TLS is enabled. Keep this key file permission-restricted and rotate per your security policy.",
tags: ["network", "storage"]
},
"gateway.tls.caPath": {
label: "Gateway TLS CA Path",
help: "Optional CA bundle path for client verification or custom trust-chain requirements at the gateway edge. Use this when private PKI or custom certificate chains are part of deployment.",
tags: ["network", "storage"]
},
"gateway.http": {
label: "Gateway HTTP API",
help: "Gateway HTTP API configuration grouping endpoint toggles and transport-facing API exposure controls. Keep only required endpoints enabled to reduce attack surface.",
tags: ["network"]
},
"gateway.http.endpoints": {
label: "Gateway HTTP Endpoints",
help: "HTTP endpoint feature toggles under the gateway API surface for compatibility routes and optional integrations. Enable endpoints intentionally and monitor access patterns after rollout.",
tags: ["network"]
},
"gateway.http.securityHeaders": {
label: "Gateway HTTP Security Headers",
help: "Optional HTTP response security headers applied by the gateway process itself. Prefer setting these at your reverse proxy when TLS terminates there.",
tags: ["network"]
},
"gateway.http.securityHeaders.strictTransportSecurity": {
label: "Strict Transport Security Header",
help: "Value for the Strict-Transport-Security response header. Set only on HTTPS origins that you fully control; use false to explicitly disable.",
tags: ["network"]
},
"gateway.remote.url": {
label: "Remote Gateway URL",
help: "Remote Gateway WebSocket URL (ws:// or wss://).",
placeholder: "ws://host:18789",
tags: ["network"]
},
"gateway.remote.sshTarget": {
label: "Remote Gateway SSH Target",
help: "Remote gateway over SSH (tunnels the gateway port to localhost). Format: user@host or user@host:port.",
placeholder: "user@host",
tags: ["network"]
},
"gateway.remote.sshIdentity": {
label: "Remote Gateway SSH Identity",
help: "Optional SSH identity file path (passed to ssh -i).",
tags: ["network"]
},
"gateway.remote.token": {
label: "Remote Gateway Token",
help: "Bearer token used to authenticate this client to a remote gateway in token-auth deployments. Store via secret/env substitution and rotate alongside remote gateway auth changes.",
tags: [
"security",
"auth",
"network"
],
sensitive: true
},
"gateway.remote.password": {
label: "Remote Gateway Password",
help: "Password credential used for remote gateway authentication when password mode is enabled. Keep this secret managed externally and avoid plaintext values in committed config.",
tags: [
"security",
"auth",
"network"
],
sensitive: true
},
"gateway.remote.tlsFingerprint": {
label: "Remote Gateway TLS Fingerprint",
help: "Expected sha256 TLS fingerprint for the remote gateway (pin to avoid MITM).",
placeholder: "sha256:ab12cd34…",
tags: [
"security",
"auth",
"network"
]
},
"gateway.auth.token": {
label: "Gateway Token",
help: "Required by default for gateway access (unless using Tailscale Serve identity); required for non-loopback binds.",
tags: [
"security",
"auth",
"access",
"network"
],
sensitive: true
},
"gateway.auth.password": {
label: "Gateway Password",
help: "Required for Tailscale funnel.",
tags: [
"security",
"auth",
"access",
"network"
],
sensitive: true
},
"browser.enabled": {
label: "Browser Enabled",
help: "Enables browser capability wiring in the gateway so browser tools and CDP-driven workflows can run. Disable when browser automation is not needed to reduce surface area and startup work.",
tags: ["advanced"]
},
"browser.cdpUrl": {
label: "Browser CDP URL",
help: "Remote CDP websocket URL used to attach to an externally managed browser instance. Use this for centralized browser hosts and keep URL access restricted to trusted network paths.",
tags: ["advanced"]
},
"browser.color": {
label: "Browser Accent Color",
help: "Default accent color used for browser profile/UI cues where colored identity hints are displayed. Use consistent colors to help operators identify active browser profile context quickly.",
tags: ["advanced"]
},
"browser.executablePath": {
label: "Browser Executable Path",
help: "Explicit browser executable path when auto-discovery is insufficient for your host environment. Use absolute stable paths so launch behavior stays deterministic across restarts.",
tags: ["storage"]
},
"browser.headless": {
label: "Browser Headless Mode",
help: "Forces browser launch in headless mode when the local launcher starts browser instances. Keep headless enabled for server environments and disable only when visible UI debugging is required.",
tags: ["advanced"]
},
"browser.noSandbox": {
label: "Browser No-Sandbox Mode",
help: "Disables Chromium sandbox isolation flags for environments where sandboxing fails at runtime. Keep this off whenever possible because process isolation protections are reduced.",
tags: ["storage"]
},
"browser.attachOnly": {
label: "Browser Attach-only Mode",
help: "Restricts browser mode to attach-only behavior without starting local browser processes. Use this when all browser sessions are externally managed by a remote CDP provider.",
tags: ["advanced"]
},
"browser.cdpPortRangeStart": {
label: "Browser CDP Port Range Start",
help: "Starting local CDP port used for auto-allocated browser profile ports. Increase this when host-level port defaults conflict with other local services.",
tags: ["advanced"]
},
"browser.defaultProfile": {
label: "Browser Default Profile",
help: "Default browser profile name selected when callers do not explicitly choose a profile. Use a stable low-privilege profile as the default to reduce accidental cross-context state use.",
tags: ["storage"]
},
"browser.profiles": {
label: "Browser Profiles",
help: "Named browser profile connection map used for explicit routing to CDP ports or URLs with optional metadata. Keep profile names consistent and avoid overlapping endpoint definitions.",
tags: ["storage"]
},
"browser.profiles.*.cdpPort": {
label: "Browser Profile CDP Port",
help: "Per-profile local CDP port used when connecting to browser instances by port instead of URL. Use unique ports per profile to avoid connection collisions.",
tags: ["storage"]
},
"browser.profiles.*.cdpUrl": {
label: "Browser Profile CDP URL",
help: "Per-profile CDP websocket URL used for explicit remote browser routing by profile name. Use this when profile connections terminate on remote hosts or tunnels.",
tags: ["storage"]
},
"browser.profiles.*.userDataDir": {
label: "Browser Profile User Data Dir",
help: "Per-profile Chromium user data directory for existing-session attachment through Chrome DevTools MCP. Use this for host-local Brave, Edge, Chromium, or non-default Chrome profiles when the built-in auto-connect path would pick the wrong browser data directory.",
tags: ["storage"]
},
"browser.profiles.*.driver": {
label: "Browser Profile Driver",
help: "Per-profile browser driver mode. Use \"openclaw\" (or legacy \"clawd\") for CDP-based profiles, or use \"existing-session\" for host-local Chrome DevTools MCP attachment.",
tags: ["storage"]
},
"browser.profiles.*.attachOnly": {
label: "Browser Profile Attach-only Mode",
help: "Per-profile attach-only override that skips local browser launch and only attaches to an existing CDP endpoint. Useful when one profile is externally managed but others are locally launched.",
tags: ["storage"]
},
"browser.profiles.*.color": {
label: "Browser Profile Accent Color",
help: "Per-profile accent color for visual differentiation in dashboards and browser-related UI hints. Use distinct colors for high-signal operator recognition of active profiles.",
tags: ["storage"]
},
"tools.allow": {
label: "Tool Allowlist",
help: "Absolute tool allowlist that replaces profile-derived defaults for strict environments. Use this only when you intentionally run a tightly curated subset of tool capabilities.",
tags: ["access", "tools"]
},
"tools.deny": {
label: "Tool Denylist",
help: "Global tool denylist that blocks listed tools even when profile or provider rules would allow them. Use deny rules for emergency lockouts and long-term defense-in-depth.",
tags: ["access", "tools"]
},
"tools.web": {
label: "Web Tools",
help: "Web-tool policy grouping for search/fetch providers, limits, and fallback behavior tuning. Keep enabled settings aligned with API key availability and outbound networking policy.",
tags: ["tools"]
},
"tools.exec": {
label: "Exec Tool",
help: "Exec-tool policy grouping for shell execution host, security mode, approval behavior, and runtime bindings. Keep conservative defaults in production and tighten elevated execution paths.",
tags: ["tools"]
},
"tools.media.image.enabled": {
label: "Enable Image Understanding",
help: "Enable image understanding so attached or referenced images can be interpreted into textual context. Disable if you need text-only operation or want to avoid image-processing cost.",
tags: ["media", "tools"]
},
"tools.media.image.maxBytes": {
label: "Image Understanding Max Bytes",
help: "Maximum accepted image payload size in bytes before the item is skipped or truncated by policy. Keep limits realistic for your provider caps and infrastructure bandwidth.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.image.maxChars": {
label: "Image Understanding Max Chars",
help: "Maximum characters returned from image understanding output after model response normalization. Use tighter limits to reduce prompt bloat and larger limits for detail-heavy OCR tasks.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.image.prompt": {
label: "Image Understanding Prompt",
help: "Instruction template used for image understanding requests to shape extraction style and detail level. Keep prompts deterministic so outputs stay consistent across turns and channels.",
tags: ["media", "tools"]
},
"tools.media.image.timeoutSeconds": {
label: "Image Understanding Timeout (sec)",
help: "Timeout in seconds for each image understanding request before it is aborted. Increase for high-resolution analysis and lower it for latency-sensitive operator workflows.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.image.attachments": {
label: "Image Understanding Attachment Policy",
help: "Attachment handling policy for image inputs, including which message attachments qualify for image analysis. Use restrictive settings in untrusted channels to reduce unexpected processing.",
tags: ["media", "tools"]
},
"tools.media.image.models": {
label: "Image Understanding Models",
help: "Ordered model preferences specifically for image understanding when you want to override shared media models. Put the most reliable multimodal model first to reduce fallback attempts.",
tags: [
"models",
"media",
"tools"
]
},
"tools.media.image.scope": {
label: "Image Understanding Scope",
help: "Scope selector for when image understanding is attempted (for example only explicit requests versus broader auto-detection). Keep narrow scope in busy channels to control token and API spend.",
tags: ["media", "tools"]
},
"tools.media.models": {
label: "Media Understanding Shared Models",
help: "Shared fallback model list used by media understanding tools when modality-specific model lists are not set. Keep this aligned with available multimodal providers to avoid runtime fallback churn.",
tags: [
"models",
"media",
"tools"
]
},
"tools.media.concurrency": {
label: "Media Understanding Concurrency",
help: "Maximum number of concurrent media understanding operations per turn across image, audio, and video tasks. Lower this in resource-constrained deployments to prevent CPU/network saturation.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.audio.enabled": {
label: "Enable Audio Understanding",
help: "Enable audio understanding so voice notes or audio clips can be transcribed/summarized for agent context. Disable when audio ingestion is outside policy or unnecessary for your workflows.",
tags: ["media", "tools"]
},
"tools.media.audio.maxBytes": {
label: "Audio Understanding Max Bytes",
help: "Maximum accepted audio payload size in bytes before processing is rejected or clipped by policy. Set this based on expected recording length and upstream provider limits.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.audio.maxChars": {
label: "Audio Understanding Max Chars",
help: "Maximum characters retained from audio understanding output to prevent oversized transcript injection. Increase for long-form dictation, or lower to keep conversational turns compact.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.audio.prompt": {
label: "Audio Understanding Prompt",
help: "Instruction template guiding audio understanding output style, such as concise summary versus near-verbatim transcript. Keep wording consistent so downstream automations can rely on output format.",
tags: ["media", "tools"]
},
"tools.media.audio.timeoutSeconds": {
label: "Audio Understanding Timeout (sec)",
help: "Timeout in seconds for audio understanding execution before the operation is cancelled. Use longer timeouts for long recordings and tighter ones for interactive chat responsiveness.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.audio.language": {
label: "Audio Understanding Language",
help: "Preferred language hint for audio understanding/transcription when provider support is available. Set this to improve recognition accuracy for known primary languages.",
tags: ["media", "tools"]
},
"tools.media.audio.attachments": {
label: "Audio Understanding Attachment Policy",
help: "Attachment policy for audio inputs indicating which uploaded files are eligible for audio processing. Keep restrictive defaults in mixed-content channels to avoid unintended audio workloads.",
tags: ["media", "tools"]
},
"tools.media.audio.models": {
label: "Audio Understanding Models",
help: "Ordered model preferences specifically for audio understanding, used before shared media model fallback. Choose models optimized for transcription quality in your primary language/domain.",
tags: [
"models",
"media",
"tools"
]
},
"tools.media.audio.scope": {
label: "Audio Understanding Scope",
help: "Scope selector for when audio understanding runs across inbound messages and attachments. Keep focused scopes in high-volume channels to reduce cost and avoid accidental transcription.",
tags: ["media", "tools"]
},
"tools.media.audio.echoTranscript": {
label: "Echo Transcript to Chat",
help: "Echo the audio transcript back to the originating chat before agent processing. When enabled, users immediately see what was heard from their voice note, helping them verify transcription accuracy before the agent acts on it. Default: false.",
tags: ["media", "tools"]
},
"tools.media.audio.echoFormat": {
label: "Transcript Echo Format",
help: "Format string for the echoed transcript message. Use `{transcript}` as a placeholder for the transcribed text. Default: '📝 \"{transcript}\"'.",
tags: ["media", "tools"]
},
"tools.media.video.enabled": {
label: "Enable Video Understanding",
help: "Enable video understanding so clips can be summarized into text for downstream reasoning and responses. Disable when processing video is out of policy or too expensive for your deployment.",
tags: ["media", "tools"]
},
"tools.media.video.maxBytes": {
label: "Video Understanding Max Bytes",
help: "Maximum accepted video payload size in bytes before policy rejection or trimming occurs. Tune this to provider and infrastructure limits to avoid repeated timeout/failure loops.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.video.maxChars": {
label: "Video Understanding Max Chars",
help: "Maximum characters retained from video understanding output to control prompt growth. Raise for dense scene descriptions and lower when concise summaries are preferred.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.video.prompt": {
label: "Video Understanding Prompt",
help: "Instruction template for video understanding describing desired summary granularity and focus areas. Keep this stable so output quality remains predictable across model/provider fallbacks.",
tags: ["media", "tools"]
},
"tools.media.video.timeoutSeconds": {
label: "Video Understanding Timeout (sec)",
help: "Timeout in seconds for each video understanding request before cancellation. Use conservative values in interactive channels and longer values for offline or batch-heavy processing.",
tags: [
"performance",
"media",
"tools"
]
},
"tools.media.video.attachments": {
label: "Video Understanding Attachment Policy",
help: "Attachment eligibility policy for video analysis, defining which message files can trigger video processing. Keep this explicit in shared channels to prevent accidental large media workloads.",
tags: ["media", "tools"]
},
"tools.media.video.models": {
label: "Video Understanding Models",
help: "Ordered model preferences specifically for video understanding before shared media fallback applies. Prioritize models with strong multimodal video support to minimize degraded summaries.",
tags: [
"models",
"media",
"tools"
]
},
"tools.media.video.scope": {
label: "Video Understanding Scope",
help: "Scope selector controlling when video understanding is attempted across incoming events. Narrow scope in noisy channels, and broaden only where video interpretation is core to workflow.",
tags: ["media", "tools"]
},
"tools.links.enabled": {
label: "Enable Link Understanding",
help: "Enable automatic link understanding pre-processing so URLs can be summarized before agent reasoning. Keep enabled for richer context, and disable when strict minimal processing is required.",
tags: ["tools"]
},
"tools.links.maxLinks": {
label: "Link Understanding Max Links",
help: "Maximum number of links expanded per turn during link understanding. Use lower values to control latency/cost in chatty threads and higher values when multi-link context is critical.",
tags: ["performance", "tools"]
},
"tools.links.timeoutSeconds": {
label: "Link Understanding Timeout (sec)",
help: "Per-link understanding timeout budget in seconds before unresolved links are skipped. Keep this bounded to avoid long stalls when external sites are slow or unreachable.",
tags: ["performance", "tools"]
},
"tools.links.models": {
label: "Link Understanding Models",
help: "Preferred model list for link understanding tasks, evaluated in order as fallbacks when supported. Use lightweight models first for routine summarization and heavier models only when needed.",
tags: ["models", "tools"]
},
"tools.links.scope": {
label: "Link Understanding Scope",
help: "Controls when link understanding runs relative to conversation context and message type. Keep scope conservative to avoid unnecessary fetches on messages where links are not actionable.",
tags: ["tools"]
},
"tools.profile": {
label: "Tool Profile",
help: "Global tool profile name used to select a predefined tool policy baseline before applying allow/deny overrides. Use this for consistent environment posture across agents and keep profile names stable.",
tags: ["storage", "tools"]
},
"tools.alsoAllow": {
label: "Tool Allowlist Additions",
help: "Extra tool allowlist entries merged on top of the selected tool profile and default policy. Keep this list small and explicit so audits can quickly identify intentional policy exceptions.",
tags: ["access", "tools"]
},
"agents.list[].tools.profile": {
label: "Agent Tool Profile",
help: "Per-agent override for tool profile selection when one agent needs a different capability baseline. Use this sparingly so policy differences across agents stay intentional and reviewable.",
tags: ["storage"]
},
"agents.list[].tools.alsoAllow": {
label: "Agent Tool Allowlist Additions",
help: "Per-agent additive allowlist for tools on top of global and profile policy. Keep narrow to avoid accidental privilege expansion on specialized agents.",
tags: ["access"]
},
"tools.byProvider": {
label: "Tool Policy by Provider",
help: "Per-provider tool allow/deny overrides keyed by channel/provider ID to tailor capabilities by surface. Use this when one provider needs stricter controls than global tool policy.",
tags: ["tools"]
},
"agents.list[].tools.byProvider": {
label: "Agent Tool Policy by Provider",
help: "Per-agent provider-specific tool policy overrides for channel-scoped capability control. Use this when a single agent needs tighter restrictions on one provider than others.",
tags: ["advanced"]
},
"tools.exec.applyPatch.enabled": {
label: "Enable apply_patch",
help: "Experimental. Enables apply_patch for OpenAI models when allowed by tool policy.",
tags: ["tools"]
},
"tools.exec.applyPatch.workspaceOnly": {
label: "apply_patch Workspace-Only",
help: "Restrict apply_patch paths to the workspace directory (default: true). Set false to allow writing outside the workspace (dangerous).",
tags: [
"security",
"access",
"tools",
"advanced"
]
},
"tools.exec.applyPatch.allowModels": {
label: "apply_patch Model Allowlist",
help: "Optional allowlist of model ids (e.g. \"gpt-5.2\" or \"openai/gpt-5.2\").",
tags: ["access", "tools"]
},
"tools.loopDetection.enabled": {
label: "Tool-loop Detection",
help: "Enable repetitive tool-call loop detection and backoff safety checks (default: false).",
tags: ["tools"]
},
"tools.loopDetection.historySize": {
label: "Tool-loop History Size",
help: "Tool history window size for loop detection (default: 30).",
tags: ["tools"]
},
"tools.loopDetection.warningThreshold": {
label: "Tool-loop Warning Threshold",
help: "Warning threshold for repetitive patterns when detector is enabled (default: 10).",
tags: ["tools"]
},
"tools.loopDetection.criticalThreshold": {
label: "Tool-loop Critical Threshold",
help: "Critical threshold for repetitive patterns when detector is enabled (default: 20).",
tags: ["tools"]
},
"tools.loopDetection.globalCircuitBreakerThreshold": {
label: "Tool-loop Global Circuit Breaker Threshold",
help: "Global no-progress breaker threshold (default: 30).",
tags: ["reliability", "tools"]
},
"tools.loopDetection.detectors.genericRepeat": {
label: "Tool-loop Generic Repeat Detection",
help: "Enable generic repeated same-tool/same-params loop detection (default: true).",
tags: ["tools"]
},
"tools.loopDetection.detectors.knownPollNoProgress": {
label: "Tool-loop Poll No-Progress Detection",
help: "Enable known poll tool no-progress loop detection (default: true).",
tags: ["tools"]
},
"tools.loopDetection.detectors.pingPong": {
label: "Tool-loop Ping-Pong Detection",
help: "Enable ping-pong loop detection (default: true).",
tags: ["tools"]
},
"tools.fs.workspaceOnly": {
label: "Workspace-only FS tools",
help: "Restrict filesystem tools (read/write/edit/apply_patch) to the workspace directory (default: false).",
tags: ["tools"]
},
"tools.sessions.visibility": {
label: "Session Tools Visibility",
help: "Controls which sessions can be targeted by sessions_list/sessions_history/sessions_send. (\"tree\" default = current session + spawned subagent sessions; \"self\" = only current; \"agent\" = any session in the current agent id; \"all\" = any session; cross-agent still requires tools.agentToAgent).",
tags: ["storage", "tools"]
},
"tools.exec.notifyOnExit": {
label: "Exec Notify On Exit",
help: "When true (default), backgrounded exec sessions on exit and node exec lifecycle events enqueue a system event and request a heartbeat.",
tags: ["tools"]
},
"tools.exec.notifyOnExitEmptySuccess": {
label: "Exec Notify On Empty Success",
help: "When true, successful backgrounded exec exits with empty output still enqueue a completion system event (default: false).",
tags: ["tools"]
},
"tools.exec.approvalRunningNoticeMs": {
label: "Exec Approval Running Notice (ms)",
help: "Delay in milliseconds before showing an in-progress notice after an exec approval is granted. Increase to reduce flicker for fast commands, or lower for quicker operator feedback.",
tags: ["tools"]
},
"tools.exec.host": {
label: "Exec Host",
help: "Selects execution host strategy for shell commands, typically controlling local vs delegated execution environment. Use the safest host mode that still satisfies your automation requirements.",
tags: ["tools"]
},
"tools.exec.security": {
label: "Exec Security",
help: "Execution security posture selector controlling sandbox/approval expectations for command execution. Keep strict security mode for untrusted prompts and relax only for trusted operator workflows.",
tags: ["tools"]
},
"tools.exec.ask": {
label: "Exec Ask",
help: "Approval strategy for when exec commands require human confirmation before running. Use stricter ask behavior in shared channels and lower-friction settings in private operator contexts.",
tags: ["tools"]
},
"tools.exec.node": {
label: "Exec Node Binding",
help: "Node binding configuration for exec tooling when command execution is delegated through connected nodes. Use explicit node binding only when multi-node routing is required.",
tags: ["tools"]
},
"tools.agentToAgent": {
label: "Agent-to-Agent Tool Access",
help: "Policy for allowing agent-to-agent tool calls and constraining which target agents can be reached. Keep disabled or tightly scoped unless cross-agent orchestration is intentionally enabled.",
tags: ["tools"]
},
"tools.agentToAgent.enabled": {
label: "Enable Agent-to-Agent Tool",
help: "Enables the agent_to_agent tool surface so one agent can invoke another agent at runtime. Keep off in simple deployments and enable only when orchestration value outweighs complexity.",
tags: ["tools"]
},
"tools.agentToAgent.allow": {
label: "Agent-to-Agent Target Allowlist",
help: "Allowlist of target agent IDs permitted for agent_to_agent calls when orchestration is enabled. Use explicit allowlists to avoid uncontrolled cross-agent call graphs.",
tags: ["access", "tools"]
},
"tools.elevated": {
label: "Elevated Tool Access",
help: "Elevated tool access controls for privileged command surfaces that should only be reachable from trusted senders. Keep disabled unless operator workflows explicitly require elevated actions.",
tags: ["tools"]
},
"tools.elevated.enabled": {
label: "Enable Elevated Tool Access",
help: "Enables elevated tool execution path when sender and policy checks pass. Keep disabled in public/shared channels and enable only for trusted owner-operated contexts.",
tags: ["tools"]
},
"tools.elevated.allowFrom": {
label: "Elevated Tool Allow Rules",
help: "Sender allow rules for elevated tools, usually keyed by channel/provider identity formats. Use narrow, explicit identities so elevated commands cannot be triggered by unintended users.",
tags: ["access", "tools"]
},
"tools.subagents": {
label: "Subagent Tool Policy",
help: "Tool policy wrapper for spawned subagents to restrict or expand tool availability compared to parent defaults. Use this to keep delegated agent capabilities scoped to task intent.",
tags: ["tools"]
},
"tools.subagents.tools": {
label: "Subagent Tool Allow/Deny Policy",
help: "Allow/deny tool policy applied to spawned subagent runtimes for per-subagent hardening. Keep this narrower than parent scope when subagents run semi-autonomous workflows.",
tags: ["tools"]
},
"tools.sandbox": {
label: "Sandbox Tool Policy",
help: "Tool policy wrapper for sandboxed agent executions so sandbox runs can have distinct capability boundaries. Use this to enforce stronger safety in sandbox contexts.",
tags: ["storage", "tools"]
},
"tools.sandbox.tools": {
label: "Sandbox Tool Allow/Deny Policy",
help: "Allow/deny tool policy applied when agents run in sandboxed execution environments. Keep policies minimal so sandbox tasks cannot escalate into unnecessary external actions.",
tags: ["storage", "tools"]
},
"tools.exec.pathPrepend": {
label: "Exec PATH Prepend",
help: "Directories to prepend to PATH for exec runs (gateway/sandbox).",
tags: ["storage", "tools"]
},
"tools.exec.safeBins": {
label: "Exec Safe Bins",
help: "Allow stdin-only safe binaries to run without explicit allowlist entries.",
tags: ["tools"]
},
"tools.exec.strictInlineEval": {
label: "Require Inline-Eval Approval",
help: "Require explicit approval for interpreter inline-eval forms such as `python -c`, `node -e`, `ruby -e`, or `osascript -e`. Prevents silent allowlist reuse and downgrades allow-always to ask-each-time for those forms.",
tags: ["tools"]
},
"tools.exec.safeBinTrustedDirs": {
label: "Exec Safe Bin Trusted Dirs",
help: "Additional explicit directories trusted for safe-bin path checks (PATH entries are never auto-trusted).",
tags: ["storage", "tools"]
},
"tools.exec.safeBinProfiles": {
label: "Exec Safe Bin Profiles",
help: "Optional per-binary safe-bin profiles (positional limits + allowed/denied flags).",
tags: ["storage", "tools"]
},
approvals: {
label: "Approvals",
help: "Approval routing controls for forwarding exec approval requests to chat destinations outside the originating session. Keep this disabled unless operators need explicit out-of-band approval visibility.",
tags: ["advanced"]
},
"approvals.exec": {
label: "Exec Approval Forwarding",
help: "Groups exec-approval forwarding behavior including enablement, routing mode, filters, and explicit targets. Configure here when approval prompts must reach operational channels instead of only the origin thread.",
tags: ["advanced"]
},
"approvals.exec.enabled": {
label: "Forward Exec Approvals",
help: "Enables forwarding of exec approval requests to configured delivery destinations (default: false). Keep disabled in low-risk setups and enable only when human approval responders need channel-visible prompts.",
tags: ["advanced"]
},
"approvals.exec.mode": {
label: "Approval Forwarding Mode",
help: "Controls where approval prompts are sent: \"session\" uses origin chat, \"targets\" uses configured targets, and \"both\" sends to both paths. Use \"session\" as baseline and expand only when operational workflow requires redundancy.",
tags: ["advanced"]
},
"approvals.exec.agentFilter": {
label: "Approval Agent Filter",
help: "Optional allowlist of agent IDs eligible for forwarded approvals, for example `[\"primary\", \"ops-agent\"]`. Use this to limit forwarding blast radius and avoid notifying channels for unrelated agents.",
tags: ["advanced"]
},
"approvals.exec.sessionFilter": {
label: "Approval Session Filter",
help: "Optional session-key filters matched as substring or regex-style patterns, for example `[\"discord:\", \"^agent:ops:\"]`. Use narrow patterns so only intended approval contexts are forwarded to shared destinations.",
tags: ["storage"]
},
"approvals.exec.targets": {
label: "Approval Forwarding Targets",
help: "Explicit delivery targets used when forwarding mode includes targets, each with channel and destination details. Keep target lists least-privilege and validate each destination before enabling broad forwarding.",
tags: ["advanced"]
},
"approvals.exec.targets[].channel": {
label: "Approval Target Channel",
help: "Channel/provider ID used for forwarded approval delivery, such as discord, slack, or a plugin channel id. Use valid channel IDs only so approvals do not silently fail due to unknown routes.",
tags: ["advanced"]
},
"approvals.exec.targets[].to": {
label: "Approval Target Destination",
help: "Destination identifier inside the target channel (channel ID, user ID, or thread root depending on provider). Verify semantics per provider because destination format differs across channel integrations.",
tags: ["advanced"]
},
"approvals.exec.targets[].accountId": {
label: "Approval Target Account ID",
help: "Optional account selector for multi-account channel setups when approvals must route through a specific account context. Use this only when the target channel has multiple configured identities.",
tags: ["advanced"]
},
"approvals.exec.targets[].threadId": {
label: "Approval Target Thread ID",
help: "Optional thread/topic target for channels that support threaded delivery of forwarded approvals. Use this to keep approval traffic contained in operational threads instead of main channels.",
tags: ["advanced"]
},
"tools.message.allowCrossContextSend": {
label: "Allow Cross-Context Messaging",
help: "Legacy override: allow cross-context sends across all providers.",
tags: ["access", "tools"]
},
"tools.message.crossContext.allowWithinProvider": {
label: "Allow Cross-Context (Same Provider)",
help: "Allow sends to other channels within the same provider (default: true).",
tags: ["access", "tools"]
},
"tools.message.crossContext.allowAcrossProviders": {
label: "Allow Cross-Context (Across Providers)",
help: "Allow sends across different providers (default: false).",
tags: ["access", "tools"]
},
"tools.message.crossContext.marker.enabled": {
label: "Cross-Context Marker",
help: "Add a visible origin marker when sending cross-context (default: true).",
tags: ["tools"]
},
"tools.message.crossContext.marker.prefix": {
label: "Cross-Context Marker Prefix",
help: "Text prefix for cross-context markers (supports \"{channel}\").",
tags: ["tools"]
},
"tools.message.crossContext.marker.suffix": {
label: "Cross-Context Marker Suffix",
help: "Text suffix for cross-context markers (supports \"{channel}\").",
tags: ["tools"]
},
"tools.message.broadcast.enabled": {
label: "Enable Message Broadcast",
help: "Enable broadcast action (default: true).",
tags: ["tools"]
},
"tools.web.search.enabled": {
label: "Enable Web Search Tool",
help: "Enable the web_search tool (requires a provider API key).",
tags: ["tools"]
},
"tools.web.search.provider": {
label: "Web Search Provider",
help: "Search provider id. Auto-detected from available API keys if omitted.",
tags: ["tools"]
},
"tools.web.search.maxResults": {
label: "Web Search Max Results",
help: "Number of results to return (1-10).",
tags: ["performance", "tools"]
},
"tools.web.search.timeoutSeconds": {
label: "Web Search Timeout (sec)",
help: "Timeout in seconds for web_search requests.",
tags: ["performance", "tools"]
},
"tools.web.search.cacheTtlMinutes": {
label: "Web Search Cache TTL (min)",
help: "Cache TTL in minutes for web_search results.",
tags: [
"performance",
"storage",
"tools"
]
},
"tools.web.fetch.enabled": {
label: "Enable Web Fetch Tool",
help: "Enable the web_fetch tool (lightweight HTTP fetch).",
tags: ["tools"]
},
"tools.web.fetch.maxChars": {
label: "Web Fetch Max Chars",
help: "Max characters returned by web_fetch (truncated).",
tags: ["performance", "tools"]
},
"tools.web.fetch.maxCharsCap": {
label: "Web Fetch Hard Max Chars",
help: "Hard cap for web_fetch maxChars (applies to config and tool calls).",
tags: ["performance", "tools"]
},
"tools.web.fetch.timeoutSeconds": {
label: "Web Fetch Timeout (sec)",
help: "Timeout in seconds for web_fetch requests.",
tags: ["performance", "tools"]
},
"tools.web.fetch.cacheTtlMinutes": {
label: "Web Fetch Cache TTL (min)",
help: "Cache TTL in minutes for web_fetch results.",
tags: [
"performance",
"storage",
"tools"
]
},
"tools.web.fetch.maxRedirects": {
label: "Web Fetch Max Redirects",
help: "Maximum redirects allowed for web_fetch (default: 3).",
tags: [
"performance",
"storage",
"tools"
]
},
"tools.web.fetch.userAgent": {
label: "Web Fetch User-Agent",
help: "Override User-Agent header for web_fetch requests.",
tags: ["tools"]
},
"tools.web.fetch.readability": {
label: "Web Fetch Readability Extraction",
help: "Use Readability to extract main content from HTML (fallbacks to basic HTML cleanup).",
tags: ["tools"]
},
"tools.web.fetch.firecrawl.enabled": {
label: "Enable Firecrawl Fallback",
help: "Enable Firecrawl fallback for web_fetch (if configured).",
tags: ["tools"]
},
"tools.web.fetch.firecrawl.apiKey": {
label: "Firecrawl API Key",
help: "Firecrawl API key (fallback: FIRECRAWL_API_KEY env var).",
tags: [
"security",
"auth",
"tools"
],
sensitive: true
},
"tools.web.fetch.firecrawl.baseUrl": {
label: "Firecrawl Base URL",
help: "Firecrawl base URL (e.g. https://api.firecrawl.dev or custom endpoint).",
tags: ["tools"]
},
"tools.web.fetch.firecrawl.onlyMainContent": {
label: "Firecrawl Main Content Only",
help: "When true, Firecrawl returns only the main content (default: true).",
tags: ["tools"]
},
"tools.web.fetch.firecrawl.maxAgeMs": {
label: "Firecrawl Cache Max Age (ms)",
help: "Firecrawl maxAge (ms) for cached results when supported by the API.",
tags: ["performance", "tools"]
},
"tools.web.fetch.firecrawl.timeoutSeconds": {
label: "Firecrawl Timeout (sec)",
help: "Timeout in seconds for Firecrawl requests.",
tags: ["performance", "tools"]
},
"gateway.controlUi.basePath": {
label: "Control UI Base Path",
help: "Optional URL prefix where the Control UI is served (e.g. /openclaw).",
placeholder: "/openclaw",
tags: ["network", "storage"]
},
"gateway.controlUi.root": {
label: "Control UI Assets Root",
help: "Optional filesystem root for Control UI assets (defaults to dist/control-ui).",
placeholder: "dist/control-ui",
tags: ["network"]
},
"gateway.controlUi.allowedOrigins": {
label: "Control UI Allowed Origins",
help: "Allowed browser origins for Control UI/WebChat websocket connections (full origins only, e.g. https://control.example.com). Required for non-loopback Control UI deployments unless dangerous Host-header fallback is explicitly enabled. Setting [\"*\"] means allow any browser origin and should be avoided outside tightly controlled local testing.",
placeholder: "https://control.example.com",
tags: ["access", "network"]
},
"gateway.controlUi.dangerouslyAllowHostHeaderOriginFallback": {
label: "Dangerously Allow Host-Header Origin Fallback",
help: "DANGEROUS toggle that enables Host-header based origin fallback for Control UI/WebChat websocket checks. This mode is supported when your deployment intentionally relies on Host-header origin policy; explicit gateway.controlUi.allowedOrigins remains the recommended hardened default.",
tags: [
"security",
"access",
"network",
"advanced"
]
},
"gateway.controlUi.allowInsecureAuth": {
label: "Insecure Control UI Auth Toggle",
help: "Loosens strict browser auth checks for Control UI when you must run a non-standard setup. Keep this off unless you trust your network and proxy path, because impersonation risk is higher.",
tags: [
"security",
"access",
"network",
"advanced"
]
},
"gateway.controlUi.dangerouslyDisableDeviceAuth": {
label: "Dangerously Disable Control UI Device Auth",
help: "Disables Control UI device identity checks and relies on token/password only. Use only for short-lived debugging on trusted networks, then turn it off immediately.",
tags: [
"security",
"access",
"network",
"advanced"
]
},
"gateway.push": {
label: "Gateway Push Delivery",
help: "Push-delivery settings used by the gateway when it needs to wake or notify paired devices. Configure relay-backed APNs here for official iOS builds; direct APNs auth remains env-based for local/manual builds.",
tags: ["network"]
},
"gateway.push.apns": {
label: "Gateway APNs Delivery",
help: "APNs delivery settings for iOS devices paired to this gateway. Use relay settings for official/TestFlight builds that register through the external push relay.",
tags: ["network"]
},
"gateway.push.apns.relay": {
label: "Gateway APNs Relay",
help: "External relay settings for relay-backed APNs sends. The gateway uses this relay for push.test, wake nudges, and reconnect wakes after a paired official iOS build publishes a relay-backed registration.",
tags: ["network"]
},
"gateway.push.apns.relay.baseUrl": {
label: "Gateway APNs Relay Base URL",
help: "Base HTTPS URL for the external APNs relay service used by official/TestFlight iOS builds. Keep this aligned with the relay URL baked into the iOS build so registration and send traffic hit the same deployment.",
placeholder: "https://relay.example.com",
tags: ["network", "advanced"]
},
"gateway.push.apns.relay.timeoutMs": {
label: "Gateway APNs Relay Timeout (ms)",
help: "Timeout in milliseconds for relay send requests from the gateway to the APNs relay (default: 10000). Increase for slower relays or networks, or lower to fail wake attempts faster.",
tags: ["network", "performance"]
},
"gateway.http.endpoints.chatCompletions.enabled": {
label: "OpenAI Chat Completions Endpoint",
help: "Enable the OpenAI-compatible `POST /v1/chat/completions` endpoint (default: false).",
tags: ["network"]
},
"gateway.http.endpoints.chatCompletions.maxBodyBytes": {
label: "OpenAI Chat Completions Max Body Bytes",
help: "Max request body size in bytes for `/v1/chat/completions` (default: 20MB).",
tags: ["network", "performance"]
},
"gateway.http.endpoints.chatCompletions.maxImageParts": {
label: "OpenAI Chat Completions Max Image Parts",
help: "Max number of `image_url` parts accepted from the latest user message (default: 8).",
tags: [
"network",
"performance",
"media"
]
},
"gateway.http.endpoints.chatCompletions.maxTotalImageBytes": {
label: "OpenAI Chat Completions Max Total Image Bytes",
help: "Max cumulative decoded bytes across all `image_url` parts in one request (default: 20MB).",
tags: [
"network",
"performance",
"media"
]
},
"gateway.http.endpoints.chatCompletions.images": {
label: "OpenAI Chat Completions Image Limits",
help: "Image fetch/validation controls for OpenAI-compatible `image_url` parts.",
tags: ["network", "media"]
},
"gateway.http.endpoints.chatCompletions.images.allowUrl": {
label: "OpenAI Chat Completions Allow Image URLs",
help: "Allow server-side URL fetches for `image_url` parts (default: false; data URIs remain supported). Set this to `false` to disable URL fetching entirely.",
tags: [
"access",
"network",
"media"
]
},
"gateway.http.endpoints.chatCompletions.images.urlAllowlist": {
label: "OpenAI Chat Completions Image URL Allowlist",
help: "Optional hostname allowlist for `image_url` URL fetches; supports exact hosts and `*.example.com` wildcards. Empty or omitted lists mean no hostname allowlist restriction.",
tags: [
"access",
"network",
"media"
]
},
"gateway.http.endpoints.chatCompletions.images.allowedMimes": {
label: "OpenAI Chat Completions Image MIME Allowlist",
help: "Allowed MIME types for `image_url` parts (case-insensitive list).",
tags: [
"access",
"network",
"media"
]
},
"gateway.http.endpoints.chatCompletions.images.maxBytes": {
label: "OpenAI Chat Completions Image Max Bytes",
help: "Max bytes per fetched/decoded `image_url` image (default: 10MB).",
tags: [
"network",
"performance",
"media"
]
},
"gateway.http.endpoints.chatCompletions.images.maxRedirects": {
label: "OpenAI Chat Completions Image Max Redirects",
help: "Max HTTP redirects allowed when fetching `image_url` URLs (default: 3).",
tags: [
"network",
"performance",
"storage",
"media"
]
},
"gateway.http.endpoints.chatCompletions.images.timeoutMs": {
label: "OpenAI Chat Completions Image Timeout (ms)",
help: "Timeout in milliseconds for `image_url` URL fetches (default: 10000).",
tags: [
"network",
"performance",
"media"
]
},
"gateway.reload.mode": {
label: "Config Reload Mode",
help: "Controls how config edits are applied: \"off\" ignores live edits, \"restart\" always restarts, \"hot\" applies in-process, and \"hybrid\" tries hot then restarts if required. Keep \"hybrid\" for safest routine updates.",
tags: ["network", "reliability"]
},
"gateway.reload.debounceMs": {
label: "Config Reload Debounce (ms)",
help: "Debounce window (ms) before applying config changes.",
tags: [
"network",
"reliability",
"performance"
]
},
"gateway.reload.deferralTimeoutMs": {
label: "Restart Deferral Timeout (ms)",
help: "Maximum time (ms) to wait for in-flight operations to complete before forcing a SIGUSR1 restart. Default: 300000 (5 minutes). Lower values risk aborting active subagent LLM calls.",
tags: [
"network",
"reliability",
"performance"
]
},
"gateway.nodes.browser.mode": {
label: "Gateway Node Browser Mode",
help: "Node browser routing (\"auto\" = pick single connected browser node, \"manual\" = require node param, \"off\" = disable).",
tags: ["network"]
},
"gateway.nodes.browser.node": {
label: "Gateway Node Browser Pin",
help: "Pin browser routing to a specific node id or name (optional).",
tags: ["network"]
},
"gateway.nodes.allowCommands": {
label: "Gateway Node Allowlist (Extra Commands)",
help: "Extra node.invoke commands to allow beyond the gateway defaults (array of command strings). Enabling dangerous commands here is a security-sensitive override and is flagged by `openclaw security audit`.",
tags: ["access", "network"]
},
"gateway.nodes.denyCommands": {
label: "Gateway Node Denylist",
help: "Node command names to block even if present in node claims or default allowlist (exact command-name matching only, e.g. `system.run`; does not inspect shell text inside that command).",
tags: ["access", "network"]
},
"nodeHost.browserProxy": {
label: "Node Browser Proxy",
help: "Groups browser-proxy settings for exposing local browser control through node routing. Enable only when remote node workflows need your local browser profiles.",
tags: ["network"]
},
"nodeHost.browserProxy.enabled": {
label: "Node Browser Proxy Enabled",
help: "Expose the local browser control server through node proxy routing so remote clients can use this host's browser capabilities. Keep disabled unless remote automation explicitly depends on it.",
tags: ["network"]
},
"nodeHost.browserProxy.allowProfiles": {
label: "Node Browser Proxy Allowed Profiles",
help: "Optional allowlist of browser profile names exposed through node proxy routing. Leave empty to preserve the default full profile surface, including profile create/delete routes. When set, OpenClaw enforces least-privilege profile access and blocks persistent profile create/delete through the proxy.",
tags: [
"access",
"network",
"storage"
]
},
media: {
label: "Media",
help: "Top-level media behavior shared across providers and tools that handle inbound files. Keep defaults unless you need stable filenames for external processing pipelines or longer-lived inbound media retention.",
tags: ["advanced"]
},
"media.preserveFilenames": {
label: "Preserve Media Filenames",
help: "When enabled, uploaded media keeps its original filename instead of a generated temp-safe name. Turn this on when downstream automations depend on stable names, and leave off to reduce accidental filename leakage.",
tags: ["storage"]
},
"media.ttlHours": {
label: "Media Retention TTL (hours)",
help: "Optional retention window in hours for persisted inbound media cleanup across the full media tree. Leave unset to preserve legacy behavior, or set values like 24 (1 day) or 168 (7 days) when you want automatic cleanup.",
tags: ["advanced"]
},
"audio.transcription": {
label: "Audio Transcription",
help: "Command-based transcription settings for converting audio files into text before agent handling. Keep a simple, deterministic command path here so failures are easy to diagnose in logs.",
tags: ["media"]
},
"audio.transcription.command": {
label: "Audio Transcription Command",
help: "Executable + args used to transcribe audio (first token must be a safe binary/path), for example `[\"whisper-cli\", \"--model\", \"small\", \"{input}\"]`. Prefer a pinned command so runtime environments behave consistently.",
tags: ["media"]
},
"audio.transcription.timeoutSeconds": {
label: "Audio Transcription Timeout (sec)",
help: "Maximum time allowed for the transcription command to finish before it is aborted. Increase this for longer recordings, and keep it tight in latency-sensitive deployments.",
tags: ["performance", "media"]
},
"bindings[].type": {
label: "Binding Type",
help: "Binding kind. Use \"route\" (or omit for legacy route entries) for normal routing, and \"acp\" for persistent ACP conversation bindings.",
tags: ["advanced"]
},
"bindings[].agentId": {
label: "Binding Agent ID",
help: "Target agent ID that receives traffic when the corresponding binding match rule is satisfied. Use valid configured agent IDs only so routing does not fail at runtime.",
tags: ["advanced"]
},
"bindings[].match": {
label: "Binding Match Rule",
help: "Match rule object for deciding when a binding applies, including channel and optional account/peer constraints. Keep rules narrow to avoid accidental agent takeover across contexts.",
tags: ["advanced"]
},
"bindings[].match.channel": {
label: "Binding Channel",
help: "Channel/provider identifier this binding applies to, such as `telegram`, `discord`, or a plugin channel ID. Use the configured channel key exactly so binding evaluation works reliably.",
tags: ["advanced"]
},
"bindings[].match.accountId": {
label: "Binding Account ID",
help: "Optional account selector for multi-account channel setups so the binding applies only to one identity. Use this when account scoping is required for the route and leave unset otherwise.",
tags: ["advanced"]
},
"bindings[].match.peer": {
label: "Binding Peer Match",
help: "Optional peer matcher for specific conversations including peer kind and peer id. Use this when only one direct/group/channel target should be pinned to an agent.",
tags: ["advanced"]
},
"bindings[].match.peer.kind": {
label: "Binding Peer Kind",
help: "Peer conversation type: \"direct\", \"group\", \"channel\", or legacy \"dm\" (deprecated alias for direct). Prefer \"direct\" for new configs and keep kind aligned with channel semantics.",
tags: ["advanced"]
},
"bindings[].match.peer.id": {
label: "Binding Peer ID",
help: "Conversation identifier used with peer matching, such as a chat ID, channel ID, or group ID from the provider. Keep this exact to avoid silent non-matches.",
tags: ["advanced"]
},
"bindings[].match.guildId": {
label: "Binding Guild ID",
help: "Optional Discord-style guild/server ID constraint for binding evaluation in multi-server deployments. Use this when the same peer identifiers can appear across different guilds.",
tags: ["advanced"]
},
"bindings[].match.teamId": {
label: "Binding Team ID",
help: "Optional team/workspace ID constraint used by providers that scope chats under teams. Add this when you need bindings isolated to one workspace context.",
tags: ["advanced"]
},
"bindings[].match.roles": {
label: "Binding Roles",
help: "Optional role-based filter list used by providers that attach roles to chat context. Use this to route privileged or operational role traffic to specialized agents.",
tags: ["advanced"]
},
"bindings[].acp": {
label: "ACP Binding Overrides",
help: "Optional per-binding ACP overrides for bindings[].type=acp. This layer overrides agents.list[].runtime.acp defaults for the matched conversation.",
tags: ["advanced"]
},
"bindings[].acp.mode": {
label: "ACP Binding Mode",
help: "ACP session mode override for this binding (persistent or oneshot).",
tags: ["advanced"]
},
"bindings[].acp.label": {
label: "ACP Binding Label",
help: "Human-friendly label for ACP status/diagnostics in this bound conversation.",
tags: ["advanced"]
},
"bindings[].acp.cwd": {
label: "ACP Binding Working Directory",
help: "Working directory override for ACP sessions created from this binding.",
tags: ["advanced"]
},
"bindings[].acp.backend": {
label: "ACP Binding Backend",
help: "ACP backend override for this binding (falls back to agent runtime ACP backend, then global acp.backend).",
tags: ["advanced"]
},
broadcast: {
label: "Broadcast",
help: "Broadcast routing map for sending the same outbound message to multiple peer IDs per source conversation. Keep this minimal and audited because one source can fan out to many destinations.",
tags: ["advanced"]
},
"broadcast.strategy": {
label: "Broadcast Strategy",
help: "Delivery order for broadcast fan-out: \"parallel\" sends to all targets concurrently, while \"sequential\" sends one-by-one. Use \"parallel\" for speed and \"sequential\" for stricter ordering/backpressure control.",
tags: ["advanced"]
},
"broadcast.*": {
label: "Broadcast Destination List",
help: "Per-source broadcast destination list where each key is a source peer ID and the value is an array of destination peer IDs. Keep lists intentional to avoid accidental message amplification.",
tags: ["advanced"]
},
"skills.load.watch": {
label: "Watch Skills",
help: "Enable filesystem watching for skill-definition changes so updates can be applied without full process restart. Keep enabled in development workflows and disable in immutable production images.",
tags: ["advanced"]
},
"skills.load.watchDebounceMs": {
label: "Skills Watch Debounce (ms)",
help: "Debounce window in milliseconds for coalescing rapid skill file changes before reload logic runs. Increase to reduce reload churn on frequent writes, or lower for faster edit feedback.",
tags: ["performance", "automation"]
},
"agents.defaults.workspace": {
label: "Workspace",
help: "Default workspace path exposed to agent runtime tools for filesystem context and repo-aware behavior. Set this explicitly when running from wrappers so path resolution stays deterministic.",
tags: ["advanced"]
},
"agents.defaults.repoRoot": {
label: "Repo Root",
help: "Optional repository root shown in the system prompt runtime line (overrides auto-detect).",
tags: ["advanced"]
},
"agents.defaults.bootstrapMaxChars": {
label: "Bootstrap Max Chars",
help: "Max characters of each workspace bootstrap file injected into the system prompt before truncation (default: 20000).",
tags: ["performance"]
},
"agents.defaults.bootstrapTotalMaxChars": {
label: "Bootstrap Total Max Chars",
help: "Max total characters across all injected workspace bootstrap files (default: 150000).",
tags: ["performance"]
},
"agents.defaults.bootstrapPromptTruncationWarning": {
label: "Bootstrap Prompt Truncation Warning",
help: "Inject agent-visible warning text when bootstrap files are truncated: \"off\", \"once\" (default), or \"always\".",
tags: ["advanced"]
},
"agents.defaults.envelopeTimezone": {
label: "Envelope Timezone",
help: "Timezone for message envelopes (\"utc\", \"local\", \"user\", or an IANA timezone string).",
tags: ["advanced"]
},
"agents.defaults.envelopeTimestamp": {
label: "Envelope Timestamp",
help: "Include absolute timestamps in message envelopes (\"on\" or \"off\").",
tags: ["advanced"]
},
"agents.defaults.envelopeElapsed": {
label: "Envelope Elapsed",
help: "Include elapsed time in message envelopes (\"on\" or \"off\").",
tags: ["advanced"]
},
"agents.defaults.memorySearch": {
label: "Memory Search",
help: "Vector search over MEMORY.md and memory/*.md (per-agent overrides supported).",
tags: ["advanced"]
},
"agents.defaults.memorySearch.enabled": {
label: "Enable Memory Search",
help: "Master toggle for memory search indexing and retrieval behavior on this agent profile. Keep enabled for semantic recall, and disable when you want fully stateless responses.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.sources": {
label: "Memory Search Sources",
help: "Chooses which sources are indexed: \"memory\" reads MEMORY.md + memory files, and \"sessions\" includes transcript history. Keep [\"memory\"] unless you need recall from prior chat transcripts.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.extraPaths": {
label: "Extra Memory Paths",
help: "Adds extra directories or .md files to the memory index beyond default memory files. Use this when key reference docs live elsewhere in your repo; when multimodal memory is enabled, matching image/audio files under these paths are also eligible for indexing.",
tags: ["storage"]
},
"agents.defaults.memorySearch.multimodal": {
label: "Memory Search Multimodal",
help: "Optional multimodal memory settings for indexing image and audio files from configured extra paths. Keep this off unless your embedding model explicitly supports cross-modal embeddings, and set `memorySearch.fallback` to \"none\" while it is enabled. Matching files are uploaded to the configured remote embedding provider during indexing.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.multimodal.enabled": {
label: "Enable Memory Search Multimodal",
help: "Enables image/audio memory indexing from extraPaths. This currently requires Gemini embedding-2, keeps the default memory roots Markdown-only, disables memory-search fallback providers, and uploads matching binary content to the configured remote embedding provider.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.multimodal.modalities": {
label: "Memory Search Multimodal Modalities",
help: "Selects which multimodal file types are indexed from extraPaths: \"image\", \"audio\", or \"all\". Keep this narrow to avoid indexing large binary corpora unintentionally.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.multimodal.maxFileBytes": {
label: "Memory Search Multimodal Max File Bytes",
help: "Sets the maximum bytes allowed per multimodal file before it is skipped during memory indexing. Use this to cap upload cost and indexing latency, or raise it for short high-quality audio clips.",
tags: ["performance", "storage"]
},
"agents.defaults.memorySearch.experimental.sessionMemory": {
label: "Memory Search Session Index (Experimental)",
help: "Indexes session transcripts into memory search so responses can reference prior chat turns. Keep this off unless transcript recall is needed, because indexing cost and storage usage both increase.",
tags: [
"security",
"storage",
"advanced"
]
},
"agents.defaults.memorySearch.provider": {
label: "Memory Search Provider",
help: "Selects the embedding backend used to build/query memory vectors: \"openai\", \"gemini\", \"voyage\", \"mistral\", \"ollama\", or \"local\". Keep your most reliable provider here and configure fallback for resilience.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.remote.baseUrl": {
label: "Remote Embedding Base URL",
help: "Overrides the embedding API endpoint, such as an OpenAI-compatible proxy or custom Gemini base URL. Use this only when routing through your own gateway or vendor endpoint; keep provider defaults otherwise.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.remote.apiKey": {
label: "Remote Embedding API Key",
help: "Supplies a dedicated API key for remote embedding calls used by memory indexing and query-time embeddings. Use this when memory embeddings should use different credentials than global defaults or environment variables.",
tags: ["security", "auth"],
sensitive: true
},
"agents.defaults.memorySearch.remote.headers": {
label: "Remote Embedding Headers",
help: "Adds custom HTTP headers to remote embedding requests, merged with provider defaults. Use this for proxy auth and tenant routing headers, and keep values minimal to avoid leaking sensitive metadata.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.remote.batch.enabled": {
label: "Remote Batch Embedding Enabled",
help: "Enables provider batch APIs for embedding jobs when supported (OpenAI/Gemini), improving throughput on larger index runs. Keep this enabled unless debugging provider batch failures or running very small workloads.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.remote.batch.wait": {
label: "Remote Batch Wait for Completion",
help: "Waits for batch embedding jobs to fully finish before the indexing operation completes. Keep this enabled for deterministic indexing state; disable only if you accept delayed consistency.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.remote.batch.concurrency": {
label: "Remote Batch Concurrency",
help: "Limits how many embedding batch jobs run at the same time during indexing (default: 2). Increase carefully for faster bulk indexing, but watch provider rate limits and queue errors.",
tags: ["performance"]
},
"agents.defaults.memorySearch.remote.batch.pollIntervalMs": {
label: "Remote Batch Poll Interval (ms)",
help: "Controls how often the system polls provider APIs for batch job status in milliseconds (default: 2000). Use longer intervals to reduce API chatter, or shorter intervals for faster completion detection.",
tags: ["performance"]
},
"agents.defaults.memorySearch.remote.batch.timeoutMinutes": {
label: "Remote Batch Timeout (min)",
help: "Sets the maximum wait time for a full embedding batch operation in minutes (default: 60). Increase for very large corpora or slower providers, and lower it to fail fast in automation-heavy flows.",
tags: ["performance"]
},
"agents.defaults.memorySearch.model": {
label: "Memory Search Model",
help: "Embedding model override used by the selected memory provider when a non-default model is required. Set this only when you need explicit recall quality/cost tuning beyond provider defaults.",
tags: ["models"]
},
"agents.defaults.memorySearch.outputDimensionality": {
label: "Memory Search Output Dimensionality",
help: "Gemini embedding-2 only: chooses the output vector size for memory embeddings. Use 768, 1536, or 3072 (default), and expect a full reindex when you change it because stored vector dimensions must stay consistent.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.fallback": {
label: "Memory Search Fallback",
help: "Backup provider used when primary embeddings fail: \"openai\", \"gemini\", \"voyage\", \"mistral\", \"ollama\", \"local\", or \"none\". Set a real fallback for production reliability; use \"none\" only if you prefer explicit failures.",
tags: ["reliability"]
},
"agents.defaults.memorySearch.local.modelPath": {
label: "Local Embedding Model Path",
help: "Specifies the local embedding model source for local memory search, such as a GGUF file path or `hf:` URI. Use this only when provider is `local`, and verify model compatibility before large index rebuilds.",
tags: ["storage"]
},
"agents.defaults.memorySearch.store.path": {
label: "Memory Search Index Path",
help: "Sets where the SQLite memory index is stored on disk for each agent. Keep the default `~/.openclaw/memory/{agentId}.sqlite` unless you need custom storage placement or backup policy alignment.",
tags: ["storage"]
},
"agents.defaults.memorySearch.store.vector.enabled": {
label: "Memory Search Vector Index",
help: "Enables the sqlite-vec extension used for vector similarity queries in memory search (default: true). Keep this enabled for normal semantic recall; disable only for debugging or fallback-only operation.",
tags: ["storage"]
},
"agents.defaults.memorySearch.store.vector.extensionPath": {
label: "Memory Search Vector Extension Path",
help: "Overrides the auto-discovered sqlite-vec extension library path (`.dylib`, `.so`, or `.dll`). Use this when your runtime cannot find sqlite-vec automatically or you pin a known-good build.",
tags: ["storage"]
},
"agents.defaults.memorySearch.chunking.tokens": {
label: "Memory Chunk Tokens",
help: "Chunk size in tokens used when splitting memory sources before embedding/indexing. Increase for broader context per chunk, or lower to improve precision on pinpoint lookups.",
tags: ["security", "auth"]
},
"agents.defaults.memorySearch.chunking.overlap": {
label: "Memory Chunk Overlap Tokens",
help: "Token overlap between adjacent memory chunks to preserve context continuity near split boundaries. Use modest overlap to reduce boundary misses without inflating index size too aggressively.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.sync.onSessionStart": {
label: "Index on Session Start",
help: "Triggers a memory index sync when a session starts so early turns see fresh memory content. Keep enabled when startup freshness matters more than initial turn latency.",
tags: ["storage", "automation"]
},
"agents.defaults.memorySearch.sync.onSearch": {
label: "Index on Search (Lazy)",
help: "Uses lazy sync by scheduling reindex on search after content changes are detected. Keep enabled for lower idle overhead, or disable if you require pre-synced indexes before any query.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.sync.watch": {
label: "Watch Memory Files",
help: "Watches memory files and schedules index updates from file-change events (chokidar). Enable for near-real-time freshness; disable on very large workspaces if watch churn is too noisy.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.sync.watchDebounceMs": {
label: "Memory Watch Debounce (ms)",
help: "Debounce window in milliseconds for coalescing rapid file-watch events before reindex runs. Increase to reduce churn on frequently-written files, or lower for faster freshness.",
tags: ["performance", "automation"]
},
"agents.defaults.memorySearch.sync.sessions.deltaBytes": {
label: "Session Delta Bytes",
help: "Requires at least this many newly appended bytes before session transcript changes trigger reindex (default: 100000). Increase to reduce frequent small reindexes, or lower for faster transcript freshness.",
tags: ["storage"]
},
"agents.defaults.memorySearch.sync.sessions.deltaMessages": {
label: "Session Delta Messages",
help: "Requires at least this many appended transcript messages before reindex is triggered (default: 50). Lower this for near-real-time transcript recall, or raise it to reduce indexing churn.",
tags: ["storage"]
},
"agents.defaults.memorySearch.sync.sessions.postCompactionForce": {
label: "Force Reindex After Compaction",
help: "Forces a session memory-search reindex after compaction-triggered transcript updates (default: true). Keep enabled when compacted summaries must be immediately searchable, or disable to reduce write-time indexing pressure.",
tags: ["storage"]
},
"agents.defaults.memorySearch.query.maxResults": {
label: "Memory Search Max Results",
help: "Maximum number of memory hits returned from search before downstream reranking and prompt injection. Raise for broader recall, or lower for tighter prompts and faster responses.",
tags: ["performance"]
},
"agents.defaults.memorySearch.query.minScore": {
label: "Memory Search Min Score",
help: "Minimum relevance score threshold for including memory results in final recall output. Increase to reduce weak/noisy matches, or lower when you need more permissive retrieval.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.enabled": {
label: "Memory Search Hybrid",
help: "Combines BM25 keyword matching with vector similarity for better recall on mixed exact + semantic queries. Keep enabled unless you are isolating ranking behavior for troubleshooting.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.vectorWeight": {
label: "Memory Search Vector Weight",
help: "Controls how strongly semantic similarity influences hybrid ranking (0-1). Increase when paraphrase matching matters more than exact terms; decrease for stricter keyword emphasis.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.textWeight": {
label: "Memory Search Text Weight",
help: "Controls how strongly BM25 keyword relevance influences hybrid ranking (0-1). Increase for exact-term matching; decrease when semantic matches should rank higher.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.candidateMultiplier": {
label: "Memory Search Hybrid Candidate Multiplier",
help: "Expands the candidate pool before reranking (default: 4). Raise this for better recall on noisy corpora, but expect more compute and slightly slower searches.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.mmr.enabled": {
label: "Memory Search MMR Re-ranking",
help: "Adds MMR reranking to diversify results and reduce near-duplicate snippets in a single answer window. Enable when recall looks repetitive; keep off for strict score ordering.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.mmr.lambda": {
label: "Memory Search MMR Lambda",
help: "Sets MMR relevance-vs-diversity balance (0 = most diverse, 1 = most relevant, default: 0.7). Lower values reduce repetition; higher values keep tightly relevant but may duplicate.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.temporalDecay.enabled": {
label: "Memory Search Temporal Decay",
help: "Applies recency decay so newer memory can outrank older memory when scores are close. Enable when timeliness matters; keep off for timeless reference knowledge.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.query.hybrid.temporalDecay.halfLifeDays": {
label: "Memory Search Temporal Decay Half-life (Days)",
help: "Controls how fast older memory loses rank when temporal decay is enabled (half-life in days, default: 30). Lower values prioritize recent context more aggressively.",
tags: ["advanced"]
},
"agents.defaults.memorySearch.cache.enabled": {
label: "Memory Search Embedding Cache",
help: "Caches computed chunk embeddings in SQLite so reindexing and incremental updates run faster (default: true). Keep this enabled unless investigating cache correctness or minimizing disk usage.",
tags: ["storage"]
},
"agents.defaults.memorySearch.cache.maxEntries": {
label: "Memory Search Embedding Cache Max Entries",
help: "Sets a best-effort upper bound on cached embeddings kept in SQLite for memory search. Use this when controlling disk growth matters more than peak reindex speed.",
tags: ["performance", "storage"]
},
memory: {
label: "Memory",
help: "Memory backend configuration (global).",
tags: ["advanced"]
},
"memory.backend": {
label: "Memory Backend",
help: "Selects the global memory engine: \"builtin\" uses OpenClaw memory internals, while \"qmd\" uses the QMD sidecar pipeline. Keep \"builtin\" unless you intentionally operate QMD.",
tags: ["storage"]
},
"memory.citations": {
label: "Memory Citations Mode",
help: "Controls citation visibility in replies: \"auto\" shows citations when useful, \"on\" always shows them, and \"off\" hides them. Keep \"auto\" for a balanced signal-to-noise default.",
tags: ["storage"]
},
"memory.qmd.command": {
label: "QMD Binary",
help: "Sets the executable path for the `qmd` binary used by the QMD backend (default: resolved from PATH). Use an explicit absolute path when multiple qmd installs exist or PATH differs across environments.",
tags: ["storage"]
},
"memory.qmd.mcporter": {
label: "QMD MCPorter",
help: "Routes QMD work through mcporter (MCP runtime) instead of spawning `qmd` for each call. Use this when cold starts are expensive on large models; keep direct process mode for simpler local setups.",
tags: ["storage"]
},
"memory.qmd.mcporter.enabled": {
label: "QMD MCPorter Enabled",
help: "Routes QMD through an mcporter daemon instead of spawning qmd per request, reducing cold-start overhead for larger models. Keep disabled unless mcporter is installed and configured.",
tags: ["storage"]
},
"memory.qmd.mcporter.serverName": {
label: "QMD MCPorter Server Name",
help: "Names the mcporter server target used for QMD calls (default: qmd). Change only when your mcporter setup uses a custom server name for qmd mcp keep-alive.",
tags: ["storage"]
},
"memory.qmd.mcporter.startDaemon": {
label: "QMD MCPorter Start Daemon",
help: "Automatically starts the mcporter daemon when mcporter-backed QMD mode is enabled (default: true). Keep enabled unless process lifecycle is managed externally by your service supervisor.",
tags: ["storage"]
},
"memory.qmd.searchMode": {
label: "QMD Search Mode",
help: "Selects the QMD retrieval path: \"query\" uses standard query flow, \"search\" uses search-oriented retrieval, and \"vsearch\" emphasizes vector retrieval. Keep default unless tuning relevance quality.",
tags: ["storage"]
},
"memory.qmd.includeDefaultMemory": {
label: "QMD Include Default Memory",
help: "Automatically indexes default memory files (MEMORY.md and memory/**/*.md) into QMD collections. Keep enabled unless you want indexing controlled only through explicit custom paths.",
tags: ["storage"]
},
"memory.qmd.paths": {
label: "QMD Extra Paths",
help: "Adds custom directories or files to include in QMD indexing, each with an optional name and glob pattern. Use this for project-specific knowledge locations that are outside default memory paths.",
tags: ["storage"]
},
"memory.qmd.paths.path": {
label: "QMD Path",
help: "Defines the root location QMD should scan, using an absolute path or `~`-relative path. Use stable directories so collection identity does not drift across environments.",
tags: ["storage"]
},
"memory.qmd.paths.pattern": {
label: "QMD Path Pattern",
help: "Filters files under each indexed root using a glob pattern, with default `**/*.md`. Use narrower patterns to reduce noise and indexing cost when directories contain mixed file types.",
tags: ["storage"]
},
"memory.qmd.paths.name": {
label: "QMD Path Name",
help: "Sets a stable collection name for an indexed path instead of deriving it from filesystem location. Use this when paths vary across machines but you want consistent collection identity.",
tags: ["storage"]
},
"memory.qmd.sessions.enabled": {
label: "QMD Session Indexing",
help: "Indexes session transcripts into QMD so recall can include prior conversation content (experimental, default: false). Enable only when transcript memory is required and you accept larger index churn.",
tags: ["storage"]
},
"memory.qmd.sessions.exportDir": {
label: "QMD Session Export Directory",
help: "Overrides where sanitized session exports are written before QMD indexing. Use this when default state storage is constrained or when exports must land on a managed volume.",
tags: ["storage"]
},
"memory.qmd.sessions.retentionDays": {
label: "QMD Session Retention (days)",
help: "Defines how long exported session files are kept before automatic pruning, in days (default: unlimited). Set a finite value for storage hygiene or compliance retention policies.",
tags: ["storage"]
},
"memory.qmd.update.interval": {
label: "QMD Update Interval",
help: "Sets how often QMD refreshes indexes from source content (duration string, default: 5m). Shorter intervals improve freshness but increase background CPU and I/O.",
tags: ["performance", "storage"]
},
"memory.qmd.update.debounceMs": {
label: "QMD Update Debounce (ms)",
help: "Sets the minimum delay between consecutive QMD refresh attempts in milliseconds (default: 15000). Increase this if frequent file changes cause update thrash or unnecessary background load.",
tags: ["performance", "storage"]
},
"memory.qmd.update.onBoot": {
label: "QMD Update on Startup",
help: "Runs an initial QMD update once during gateway startup (default: true). Keep enabled so recall starts from a fresh baseline; disable only when startup speed is more important than immediate freshness.",
tags: ["storage"]
},
"memory.qmd.update.waitForBootSync": {
label: "QMD Wait for Boot Sync",
help: "Blocks startup completion until the initial boot-time QMD sync finishes (default: false). Enable when you need fully up-to-date recall before serving traffic, and keep off for faster boot.",
tags: ["storage"]
},
"memory.qmd.update.embedInterval": {
label: "QMD Embed Interval",
help: "Sets how often QMD recomputes embeddings (duration string, default: 60m; set 0 to disable periodic embeds). Lower intervals improve freshness but increase embedding workload and cost.",
tags: ["performance", "storage"]
},
"memory.qmd.update.commandTimeoutMs": {
label: "QMD Command Timeout (ms)",
help: "Sets timeout for QMD maintenance commands such as collection list/add in milliseconds (default: 30000). Increase when running on slower disks or remote filesystems that delay command completion.",
tags: ["performance", "storage"]
},
"memory.qmd.update.updateTimeoutMs": {
label: "QMD Update Timeout (ms)",
help: "Sets maximum runtime for each `qmd update` cycle in milliseconds (default: 120000). Raise this for larger collections; lower it when you want quicker failure detection in automation.",
tags: ["performance", "storage"]
},
"memory.qmd.update.embedTimeoutMs": {
label: "QMD Embed Timeout (ms)",
help: "Sets maximum runtime for each `qmd embed` cycle in milliseconds (default: 120000). Increase for heavier embedding workloads or slower hardware, and lower to fail fast under tight SLAs.",
tags: ["performance", "storage"]
},
"memory.qmd.limits.maxResults": {
label: "QMD Max Results",
help: "Limits how many QMD hits are returned into the agent loop for each recall request (default: 6). Increase for broader recall context, or lower to keep prompts tighter and faster.",
tags: ["performance", "storage"]
},
"memory.qmd.limits.maxSnippetChars": {
label: "QMD Max Snippet Chars",
help: "Caps per-result snippet length extracted from QMD hits in characters (default: 700). Lower this when prompts bloat quickly, and raise only if answers consistently miss key details.",
tags: ["performance", "storage"]
},
"memory.qmd.limits.maxInjectedChars": {
label: "QMD Max Injected Chars",
help: "Caps how much QMD text can be injected into one turn across all hits. Use lower values to control prompt bloat and latency; raise only when context is consistently truncated.",
tags: ["performance", "storage"]
},
"memory.qmd.limits.timeoutMs": {
label: "QMD Search Timeout (ms)",
help: "Sets per-query QMD search timeout in milliseconds (default: 4000). Increase for larger indexes or slower environments, and lower to keep request latency bounded.",
tags: ["performance", "storage"]
},
"memory.qmd.scope": {
label: "QMD Surface Scope",
help: "Defines which sessions/channels are eligible for QMD recall using session.sendPolicy-style rules. Keep default direct-only scope unless you intentionally want cross-chat memory sharing.",
tags: ["storage"]
},
auth: {
label: "Auth",
help: "Authentication profile root used for multi-profile provider credentials and cooldown-based failover ordering. Keep profiles minimal and explicit so automatic failover behavior stays auditable.",
tags: ["advanced"]
},
"auth.profiles": {
label: "Auth Profiles",
help: "Named auth profiles (provider + mode + optional email).",
tags: [
"auth",
"access",
"storage"
]
},
"auth.order": {
label: "Auth Profile Order",
help: "Ordered auth profile IDs per provider (used for automatic failover).",
tags: ["auth", "access"]
},
"auth.cooldowns": {
label: "Auth Cooldowns",
help: "Cooldown/backoff controls for temporary profile suppression after billing-related failures and retry windows. Use these to prevent rapid re-selection of profiles that are still blocked.",
tags: ["auth", "access"]
},
acp: {
label: "ACP",
help: "ACP runtime controls for enabling dispatch, selecting backends, constraining allowed agent targets, and tuning streamed turn projection behavior.",
tags: ["advanced"]
},
"acp.enabled": {
label: "ACP Enabled",
help: "Global ACP feature gate. Keep disabled unless ACP runtime + policy are configured.",
tags: ["advanced"]
},
"acp.dispatch.enabled": {
label: "ACP Dispatch Enabled",
help: "Independent dispatch gate for ACP session turns (default: true). Set false to keep ACP commands available while blocking ACP turn execution.",
tags: ["advanced"]
},
"acp.backend": {
label: "ACP Backend",
help: "Default ACP runtime backend id (for example: acpx). Must match a registered ACP runtime plugin backend.",
tags: ["advanced"]
},
"acp.defaultAgent": {
label: "ACP Default Agent",
help: "Fallback ACP target agent id used when ACP spawns do not specify an explicit target.",
tags: ["advanced"]
},
"acp.allowedAgents": {
label: "ACP Allowed Agents",
help: "Allowlist of ACP target agent ids permitted for ACP runtime sessions. Empty means no additional allowlist restriction.",
tags: ["access"]
},
"acp.maxConcurrentSessions": {
label: "ACP Max Concurrent Sessions",
help: "Maximum concurrently active ACP sessions across this gateway process.",
tags: ["performance", "storage"]
},
"acp.stream": {
label: "ACP Stream",
help: "ACP streaming projection controls for chunk sizing, metadata visibility, and deduped delivery behavior.",
tags: ["advanced"]
},
"acp.stream.coalesceIdleMs": {
label: "ACP Stream Coalesce Idle (ms)",
help: "Coalescer idle flush window in milliseconds for ACP streamed text before block replies are emitted.",
tags: ["advanced"]
},
"acp.stream.maxChunkChars": {
label: "ACP Stream Max Chunk Chars",
help: "Maximum chunk size for ACP streamed block projection before splitting into multiple block replies.",
tags: ["performance"]
},
"acp.stream.repeatSuppression": {
label: "ACP Stream Repeat Suppression",
help: "When true (default), suppress repeated ACP status/tool projection lines in a turn while keeping raw ACP events unchanged.",
tags: ["advanced"]
},
"acp.stream.deliveryMode": {
label: "ACP Stream Delivery Mode",
help: "ACP delivery style: live streams projected output incrementally, final_only buffers all projected ACP output until terminal turn events.",
tags: ["advanced"]
},
"acp.stream.hiddenBoundarySeparator": {
label: "ACP Stream Hidden Boundary Separator",
help: "Separator inserted before next visible assistant text when hidden ACP tool lifecycle events occurred (none|space|newline|paragraph). Default: paragraph.",
tags: ["advanced"]
},
"acp.stream.maxOutputChars": {
label: "ACP Stream Max Output Chars",
help: "Maximum assistant output characters projected per ACP turn before truncation notice is emitted.",
tags: ["performance"]
},
"acp.stream.maxSessionUpdateChars": {
label: "ACP Stream Max Session Update Chars",
help: "Maximum characters for projected ACP session/update lines (tool/status updates).",
tags: ["performance", "storage"]
},
"acp.stream.tagVisibility": {
label: "ACP Stream Tag Visibility",
help: "Per-sessionUpdate visibility overrides for ACP projection (for example usage_update, available_commands_update).",
tags: ["advanced"]
},
"acp.runtime.ttlMinutes": {
label: "ACP Runtime TTL (minutes)",
help: "Idle runtime TTL in minutes for ACP session workers before eligible cleanup.",
tags: ["advanced"]
},
"acp.runtime.installCommand": {
label: "ACP Runtime Install Command",
help: "Optional operator install/setup command shown by `/acp install` and `/acp doctor` when ACP backend wiring is missing.",
tags: ["advanced"]
},
"models.mode": {
label: "Model Catalog Mode",
help: "Controls provider catalog behavior: \"merge\" keeps built-ins and overlays your custom providers, while \"replace\" uses only your configured providers. In \"merge\", matching provider IDs preserve non-empty agent models.json baseUrl values, while apiKey values are preserved only when the provider is not SecretRef-managed in current config/auth-profile context; SecretRef-managed providers refresh apiKey from current source markers, and matching model contextWindow/maxTokens use the higher value between explicit and implicit entries.",
tags: ["models"]
},
"models.providers": {
label: "Model Providers",
help: "Provider map keyed by provider ID containing connection/auth settings and concrete model definitions. Use stable provider keys so references from agents and tooling remain portable across environments.",
tags: ["models"]
},
"models.providers.*.baseUrl": {
label: "Model Provider Base URL",
help: "Base URL for the provider endpoint used to serve model requests for that provider entry. Use HTTPS endpoints and keep URLs environment-specific through config templating where needed.",
tags: ["models"]
},
"models.providers.*.apiKey": {
label: "Model Provider API Key",
help: "Provider credential used for API-key based authentication when the provider requires direct key auth. Use secret/env substitution and avoid storing real keys in committed config files.",
tags: [
"security",
"auth",
"models"
],
sensitive: true
},
"models.providers.*.auth": {
label: "Model Provider Auth Mode",
help: "Selects provider auth style: \"api-key\" for API key auth, \"token\" for bearer token auth, \"oauth\" for OAuth credentials, and \"aws-sdk\" for AWS credential resolution. Match this to your provider requirements.",
tags: ["models"]
},
"models.providers.*.api": {
label: "Model Provider API Adapter",
help: "Provider API adapter selection controlling request/response compatibility handling for model calls. Use the adapter that matches your upstream provider protocol to avoid feature mismatch.",
tags: ["models"]
},
"models.providers.*.injectNumCtxForOpenAICompat": {
label: "Model Provider Inject num_ctx (OpenAI Compat)",
help: "Controls whether OpenClaw injects `options.num_ctx` for Ollama providers configured with the OpenAI-compatible adapter (`openai-completions`). Default is true. Set false only if your proxy/upstream rejects unknown `options` payload fields.",
tags: ["models"]
},
"models.providers.*.headers": {
label: "Model Provider Headers",
help: "Static HTTP headers merged into provider requests for tenant routing, proxy auth, or custom gateway requirements. Use this sparingly and keep sensitive header values in secrets.",
tags: ["models"]
},
"models.providers.*.authHeader": {
label: "Model Provider Authorization Header",
help: "When true, credentials are sent via the HTTP Authorization header even if alternate auth is possible. Use this only when your provider or proxy explicitly requires Authorization forwarding.",
tags: ["models"]
},
"models.providers.*.models": {
label: "Model Provider Model List",
help: "Declared model list for a provider including identifiers, metadata, and optional compatibility/cost hints. Keep IDs exact to provider catalog values so selection and fallback resolve correctly.",
tags: ["models"]
},
"models.bedrockDiscovery": {
label: "Bedrock Model Discovery",
help: "Automatic AWS Bedrock model discovery settings used to synthesize provider model entries from account visibility. Keep discovery scoped and refresh intervals conservative to reduce API churn.",
tags: ["models"]
},
"models.bedrockDiscovery.enabled": {
label: "Bedrock Discovery Enabled",
help: "Enables periodic Bedrock model discovery and catalog refresh for Bedrock-backed providers. Keep disabled unless Bedrock is actively used and IAM permissions are correctly configured.",
tags: ["models"]
},
"models.bedrockDiscovery.region": {
label: "Bedrock Discovery Region",
help: "AWS region used for Bedrock discovery calls when discovery is enabled for your deployment. Use the region where your Bedrock models are provisioned to avoid empty discovery results.",
tags: ["models"]
},
"models.bedrockDiscovery.providerFilter": {
label: "Bedrock Discovery Provider Filter",
help: "Optional provider allowlist filter for Bedrock discovery so only selected providers are refreshed. Use this to limit discovery scope in multi-provider environments.",
tags: ["models"]
},
"models.bedrockDiscovery.refreshInterval": {
label: "Bedrock Discovery Refresh Interval (s)",
help: "Refresh cadence for Bedrock discovery polling in seconds to detect newly available models over time. Use longer intervals in production to reduce API cost and control-plane noise.",
tags: ["performance", "models"]
},
"models.bedrockDiscovery.defaultContextWindow": {
label: "Bedrock Default Context Window",
help: "Fallback context-window value applied to discovered models when provider metadata lacks explicit limits. Use realistic defaults to avoid oversized prompts that exceed true provider constraints.",
tags: ["models"]
},
"models.bedrockDiscovery.defaultMaxTokens": {
label: "Bedrock Default Max Tokens",
help: "Fallback max-token value applied to discovered models without explicit output token limits. Use conservative defaults to reduce truncation surprises and unexpected token spend.",
tags: [
"security",
"auth",
"performance",
"models"
]
},
"auth.cooldowns.billingBackoffHours": {
label: "Billing Backoff (hours)",
help: "Base backoff (hours) when a profile fails due to billing/insufficient credits (default: 5).",
tags: [
"auth",
"access",
"reliability"
]
},
"auth.cooldowns.billingBackoffHoursByProvider": {
label: "Billing Backoff Overrides",
help: "Optional per-provider overrides for billing backoff (hours).",
tags: [
"auth",
"access",
"reliability"
]
},
"auth.cooldowns.billingMaxHours": {
label: "Billing Backoff Cap (hours)",
help: "Cap (hours) for billing backoff (default: 24).",
tags: [
"auth",
"access",
"performance"
]
},
"auth.cooldowns.failureWindowHours": {
label: "Failover Window (hours)",
help: "Failure window (hours) for backoff counters (default: 24).",
tags: ["auth", "access"]
},
"agents.defaults.models": {
label: "Models",
help: "Configured model catalog (keys are full provider/model IDs).",
tags: ["models"]
},
"agents.defaults.model.primary": {
label: "Primary Model",
help: "Primary model (provider/model).",
tags: ["models"]
},
"agents.defaults.model.fallbacks": {
label: "Model Fallbacks",
help: "Ordered fallback models (provider/model). Used when the primary model fails.",
tags: ["reliability", "models"]
},
"agents.defaults.imageModel.primary": {
label: "Image Model",
help: "Optional image model (provider/model) used when the primary model lacks image input.",
tags: ["models", "media"]
},
"agents.defaults.imageModel.fallbacks": {
label: "Image Model Fallbacks",
help: "Ordered fallback image models (provider/model).",
tags: [
"reliability",
"models",
"media"
]
},
"agents.defaults.imageGenerationModel.primary": {
label: "Image Generation Model",
help: "Optional image-generation model (provider/model) used by the shared image generation capability.",
tags: ["media"]
},
"agents.defaults.imageGenerationModel.fallbacks": {
label: "Image Generation Model Fallbacks",
help: "Ordered fallback image-generation models (provider/model).",
tags: ["reliability", "media"]
},
"agents.defaults.pdfModel.primary": {
label: "PDF Model",
help: "Optional PDF model (provider/model) for the PDF analysis tool. Defaults to imageModel, then session model.",
tags: ["advanced"]
},
"agents.defaults.pdfModel.fallbacks": {
label: "PDF Model Fallbacks",
help: "Ordered fallback PDF models (provider/model).",
tags: ["reliability"]
},
"agents.defaults.pdfMaxBytesMb": {
label: "PDF Max Size (MB)",
help: "Maximum PDF file size in megabytes for the PDF tool (default: 10).",
tags: ["performance"]
},
"agents.defaults.pdfMaxPages": {
label: "PDF Max Pages",
help: "Maximum number of PDF pages to process for the PDF tool (default: 20).",
tags: ["performance"]
},
"agents.defaults.imageMaxDimensionPx": {
label: "Image Max Dimension (px)",
help: "Max image side length in pixels when sanitizing transcript/tool-result image payloads (default: 1200).",
tags: ["performance", "media"]
},
"agents.defaults.humanDelay.mode": {
label: "Human Delay Mode",
help: "Delay style for block replies (\"off\", \"natural\", \"custom\").",
tags: ["advanced"]
},
"agents.defaults.humanDelay.minMs": {
label: "Human Delay Min (ms)",
help: "Minimum delay in ms for custom humanDelay (default: 800).",
tags: ["advanced"]
},
"agents.defaults.humanDelay.maxMs": {
label: "Human Delay Max (ms)",
help: "Maximum delay in ms for custom humanDelay (default: 2500).",
tags: ["performance"]
},
"agents.defaults.cliBackends": {
label: "CLI Backends",
help: "Optional CLI backends for text-only fallback (claude-cli, etc.).",
tags: ["advanced"]
},
"agents.defaults.compaction": {
label: "Compaction",
help: "Compaction tuning for when context nears token limits, including history share, reserve headroom, and pre-compaction memory flush behavior. Use this when long-running sessions need stable continuity under tight context windows.",
tags: ["advanced"]
},
"agents.defaults.compaction.mode": {
label: "Compaction Mode",
help: "Compaction strategy mode: \"default\" uses baseline behavior, while \"safeguard\" applies stricter guardrails to preserve recent context. Keep \"default\" unless you observe aggressive history loss near limit boundaries.",
tags: ["advanced"]
},
"agents.defaults.compaction.reserveTokens": {
label: "Compaction Reserve Tokens",
help: "Token headroom reserved for reply generation and tool output after compaction runs. Use higher reserves for verbose/tool-heavy sessions, and lower reserves when maximizing retained history matters more.",
tags: ["security", "auth"]
},
"agents.defaults.compaction.keepRecentTokens": {
label: "Compaction Keep Recent Tokens",
help: "Minimum token budget preserved from the most recent conversation window during compaction. Use higher values to protect immediate context continuity and lower values to keep more long-tail history.",
tags: ["security", "auth"]
},
"agents.defaults.compaction.reserveTokensFloor": {
label: "Compaction Reserve Token Floor",
help: "Minimum floor enforced for reserveTokens in Pi compaction paths (0 disables the floor guard). Use a non-zero floor to avoid over-aggressive compression under fluctuating token estimates.",
tags: ["security", "auth"]
},
"agents.defaults.compaction.maxHistoryShare": {
label: "Compaction Max History Share",
help: "Maximum fraction of total context budget allowed for retained history after compaction (range 0.1-0.9). Use lower shares for more generation headroom or higher shares for deeper historical continuity.",
tags: ["performance"]
},
"agents.defaults.compaction.identifierPolicy": {
label: "Compaction Identifier Policy",
help: "Identifier-preservation policy for compaction summaries: \"strict\" prepends built-in opaque-identifier retention guidance (default), \"off\" disables this prefix, and \"custom\" uses identifierInstructions. Keep \"strict\" unless you have a specific compatibility need.",
tags: ["access"]
},
"agents.defaults.compaction.identifierInstructions": {
label: "Compaction Identifier Instructions",
help: "Custom identifier-preservation instruction text used when identifierPolicy=\"custom\". Keep this explicit and safety-focused so compaction summaries do not rewrite opaque IDs, URLs, hosts, or ports.",
tags: ["advanced"]
},
"agents.defaults.compaction.recentTurnsPreserve": {
label: "Compaction Preserve Recent Turns",
help: "Number of most recent user/assistant turns kept verbatim outside safeguard summarization (default: 3). Raise this to preserve exact recent dialogue context, or lower it to maximize compaction savings.",
tags: ["advanced"]
},
"agents.defaults.compaction.qualityGuard": {
label: "Compaction Quality Guard",
help: "Optional quality-audit retry settings for safeguard compaction summaries. Leave this disabled unless you explicitly want summary audits and one-shot regeneration on failed checks.",
tags: ["advanced"]
},
"agents.defaults.compaction.qualityGuard.enabled": {
label: "Compaction Quality Guard Enabled",
help: "Enables summary quality audits and regeneration retries for safeguard compaction. Default: false, so safeguard mode alone does not turn on retry behavior.",
tags: ["advanced"]
},
"agents.defaults.compaction.qualityGuard.maxRetries": {
label: "Compaction Quality Guard Max Retries",
help: "Maximum number of regeneration retries after a failed safeguard summary quality audit. Use small values to bound extra latency and token cost.",
tags: ["performance"]
},
"agents.defaults.compaction.postIndexSync": {
label: "Compaction Post-Index Sync",
help: "Controls post-compaction session memory reindex mode: \"off\", \"async\", or \"await\" (default: \"async\"). Use \"await\" for strongest freshness, \"async\" for lower compaction latency, and \"off\" only when session-memory sync is handled elsewhere.",
tags: ["advanced"]
},
"agents.defaults.compaction.postCompactionSections": {
label: "Post-Compaction Context Sections",
help: "AGENTS.md H2/H3 section names re-injected after compaction so the agent reruns critical startup guidance. Leave unset to use \"Session Startup\"/\"Red Lines\" with legacy fallback to \"Every Session\"/\"Safety\"; set to [] to disable reinjection entirely.",
tags: ["advanced"]
},
"agents.defaults.compaction.timeoutSeconds": {
label: "Compaction Timeout (Seconds)",
help: "Maximum time in seconds allowed for a single compaction operation before it is aborted (default: 900). Increase this for very large sessions that need more time to summarize, or decrease it to fail faster on unresponsive models.",
tags: ["performance"]
},
"agents.defaults.compaction.model": {
label: "Compaction Model Override",
help: "Optional provider/model override used only for compaction summarization. Set this when you want compaction to run on a different model than the session default, and leave it unset to keep using the primary agent model.",
tags: ["models"]
},
"agents.defaults.compaction.truncateAfterCompaction": {
label: "Truncate After Compaction",
help: "When enabled, rewrites the session JSONL file after compaction to remove entries that were summarized. Prevents unbounded file growth in long-running sessions with many compaction cycles. Default: false.",
tags: ["advanced"]
},
"agents.defaults.compaction.memoryFlush": {
label: "Compaction Memory Flush",
help: "Pre-compaction memory flush settings that run an agentic memory write before heavy compaction. Keep enabled for long sessions so salient context is persisted before aggressive trimming.",
tags: ["advanced"]
},
"agents.defaults.compaction.memoryFlush.enabled": {
label: "Compaction Memory Flush Enabled",
help: "Enables pre-compaction memory flush before the runtime performs stronger history reduction near token limits. Keep enabled unless you intentionally disable memory side effects in constrained environments.",
tags: ["advanced"]
},
"agents.defaults.compaction.memoryFlush.softThresholdTokens": {
label: "Compaction Memory Flush Soft Threshold",
help: "Threshold distance to compaction (in tokens) that triggers pre-compaction memory flush execution. Use earlier thresholds for safer persistence, or tighter thresholds for lower flush frequency.",
tags: ["security", "auth"]
},
"agents.defaults.compaction.memoryFlush.forceFlushTranscriptBytes": {
label: "Compaction Memory Flush Transcript Size Threshold",
help: "Forces pre-compaction memory flush when transcript file size reaches this threshold (bytes or strings like \"2mb\"). Use this to prevent long-session hangs even when token counters are stale; set to 0 to disable.",
tags: ["advanced"]
},
"agents.defaults.compaction.memoryFlush.prompt": {
label: "Compaction Memory Flush Prompt",
help: "User-prompt template used for the pre-compaction memory flush turn when generating memory candidates. Use this only when you need custom extraction instructions beyond the default memory flush behavior.",
tags: ["advanced"]
},
"agents.defaults.compaction.memoryFlush.systemPrompt": {
label: "Compaction Memory Flush System Prompt",
help: "System-prompt override for the pre-compaction memory flush turn to control extraction style and safety constraints. Use carefully so custom instructions do not reduce memory quality or leak sensitive context.",
tags: ["advanced"]
},
"agents.defaults.embeddedPi": {
label: "Embedded Pi",
help: "Embedded Pi runner hardening controls for how workspace-local Pi settings are trusted and applied in OpenClaw sessions.",
tags: ["advanced"]
},
"agents.defaults.embeddedPi.projectSettingsPolicy": {
label: "Embedded Pi Project Settings Policy",
help: "How embedded Pi handles workspace-local `.pi/config/settings.json`: \"sanitize\" (default) strips shellPath/shellCommandPrefix, \"ignore\" disables project settings entirely, and \"trusted\" applies project settings as-is.",
tags: ["access"]
},
"agents.defaults.heartbeat.directPolicy": {
label: "Heartbeat Direct Policy",
help: "Controls whether heartbeat delivery may target direct/DM chats: \"allow\" (default) permits DM delivery and \"block\" suppresses direct-target sends.",
tags: [
"access",
"storage",
"automation"
]
},
"agents.list.*.heartbeat.directPolicy": {
label: "Heartbeat Direct Policy",
help: "Per-agent override for heartbeat direct/DM delivery policy; use \"block\" for agents that should only send heartbeat alerts to non-DM destinations.",
tags: [
"access",
"storage",
"automation"
]
},
"agents.defaults.heartbeat.suppressToolErrorWarnings": {
label: "Heartbeat Suppress Tool Error Warnings",
help: "Suppress tool error warning payloads during heartbeat runs.",
tags: ["automation"]
},
"agents.defaults.sandbox.browser.network": {
label: "Sandbox Browser Network",
help: "Docker network for sandbox browser containers (default: openclaw-sandbox-browser). Avoid bridge if you need stricter isolation.",
tags: ["storage"]
},
"agents.defaults.sandbox.browser.cdpSourceRange": {
label: "Sandbox Browser CDP Source Port Range",
help: "Optional CIDR allowlist for container-edge CDP ingress (for example 172.21.0.1/32).",
tags: ["storage"]
},
"agents.defaults.sandbox.docker.dangerouslyAllowContainerNamespaceJoin": {
label: "Sandbox Docker Allow Container Namespace Join",
help: "DANGEROUS break-glass override that allows sandbox Docker network mode container:<id>. This joins another container namespace and weakens sandbox isolation.",
tags: [
"security",
"access",
"storage",
"advanced"
]
},
"commands.native": {
label: "Native Commands",
help: "Registers native slash/menu commands with channels that support command registration (Discord, Slack, Telegram). Keep enabled for discoverability unless you intentionally run text-only command workflows.",
tags: ["advanced"]
},
"commands.nativeSkills": {
label: "Native Skill Commands",
help: "Registers native skill commands so users can invoke skills directly from provider command menus where supported. Keep aligned with your skill policy so exposed commands match what operators expect.",
tags: ["advanced"]
},
"commands.text": {
label: "Text Commands",
help: "Enables text-command parsing in chat input in addition to native command surfaces where available. Keep this enabled for compatibility across channels that do not support native command registration.",
tags: ["advanced"]
},
"commands.bash": {
label: "Allow Bash Chat Command",
help: "Allow bash chat command (`!`; `/bash` alias) to run host shell commands (default: false; requires tools.elevated).",
tags: ["advanced"]
},
"commands.bashForegroundMs": {
label: "Bash Foreground Window (ms)",
help: "How long bash waits before backgrounding (default: 2000; 0 backgrounds immediately).",
tags: ["advanced"]
},
"commands.config": {
label: "Allow /config",
help: "Allow /config chat command to read/write config on disk (default: false).",
tags: ["advanced"]
},
"commands.mcp": {
label: "Allow /mcp",
help: "Allow /mcp chat command to manage OpenClaw MCP server config under mcp.servers (default: false).",
tags: ["advanced"]
},
"commands.plugins": {
label: "Allow /plugins",
help: "Allow /plugins chat command to list discovered plugins and toggle plugin enablement in config (default: false).",
tags: ["advanced"]
},
"commands.debug": {
label: "Allow /debug",
help: "Allow /debug chat command for runtime-only overrides (default: false).",
tags: ["advanced"]
},
"commands.restart": {
label: "Allow Restart",
help: "Allow /restart and gateway restart tool actions (default: true).",
tags: ["advanced"]
},
"commands.useAccessGroups": {
label: "Use Access Groups",
help: "Enforce access-group allowlists/policies for commands.",
tags: ["access"]
},
"commands.ownerAllowFrom": {
label: "Command Owners",
help: "Explicit owner allowlist for owner-only tools/commands. Use channel-native IDs (optionally prefixed like \"whatsapp:+15551234567\"). '*' is ignored.",
tags: ["access"]
},
"commands.ownerDisplay": {
label: "Owner ID Display",
help: "Controls how owner IDs are rendered in the system prompt. Allowed values: raw, hash. Default: raw.",
tags: ["access"]
},
"commands.ownerDisplaySecret": {
label: "Owner ID Hash Secret",
help: "Optional secret used to HMAC hash owner IDs when ownerDisplay=hash. Prefer env substitution.",
tags: [
"security",
"auth",
"access"
],
sensitive: true
},
"commands.allowFrom": {
label: "Command Elevated Access Rules",
help: "Defines elevated command allow rules by channel and sender for owner-level command surfaces. Use narrow provider-specific identities so privileged commands are not exposed to broad chat audiences.",
tags: ["access"]
},
mcp: {
label: "MCP",
help: "Global MCP server definitions managed by OpenClaw. Embedded Pi and other runtime adapters can consume these servers without storing them inside Pi-owned project settings.",
tags: ["advanced"]
},
"mcp.servers": {
label: "MCP Servers",
help: "Named MCP server definitions. OpenClaw stores them in its own config and runtime adapters decide which transports are supported at execution time.",
tags: ["advanced"]
},
"ui.seamColor": {
label: "Accent Color",
help: "Primary accent color used by UI surfaces for emphasis, badges, and visual identity cues. Use high-contrast values that remain readable across light/dark themes.",
tags: ["advanced"]
},
"ui.assistant": {
label: "Assistant Appearance",
help: "Assistant display identity settings for name and avatar shown in UI surfaces. Keep these values aligned with your operator-facing persona and support expectations.",
tags: ["advanced"]
},
"ui.assistant.name": {
label: "Assistant Name",
help: "Display name shown for the assistant in UI views, chat chrome, and status contexts. Keep this stable so operators can reliably identify which assistant persona is active.",
tags: ["advanced"]
},
"ui.assistant.avatar": {
label: "Assistant Avatar",
help: "Assistant avatar image source used in UI surfaces (URL, path, or data URI depending on runtime support). Use trusted assets and consistent branding dimensions for clean rendering.",
tags: ["advanced"]
},
"browser.evaluateEnabled": {
label: "Browser Evaluate Enabled",
help: "Enables browser-side evaluate helpers for runtime script evaluation capabilities where supported. Keep disabled unless your workflows require evaluate semantics beyond snapshots/navigation.",
tags: ["advanced"]
},
"browser.snapshotDefaults": {
label: "Browser Snapshot Defaults",
help: "Default snapshot capture configuration used when callers do not provide explicit snapshot options. Tune this for consistent capture behavior across channels and automation paths.",
tags: ["advanced"]
},
"browser.snapshotDefaults.mode": {
label: "Browser Snapshot Mode",
help: "Default snapshot extraction mode controlling how page content is transformed for agent consumption. Choose the mode that balances readability, fidelity, and token footprint for your workflows.",
tags: ["advanced"]
},
"browser.ssrfPolicy": {
label: "Browser SSRF Policy",
help: "Server-side request forgery guardrail settings for browser/network fetch paths that could reach internal hosts. Keep restrictive defaults in production and open only explicitly approved targets.",
tags: ["access"]
},
"browser.ssrfPolicy.allowPrivateNetwork": {
label: "Browser Allow Private Network",
help: "Legacy alias for browser.ssrfPolicy.dangerouslyAllowPrivateNetwork. Prefer the dangerously-named key so risk intent is explicit.",
tags: ["access"]
},
"browser.ssrfPolicy.dangerouslyAllowPrivateNetwork": {
label: "Browser Dangerously Allow Private Network",
help: "Allows access to private-network address ranges from browser tooling. Default is enabled for trusted-network operator setups; disable to enforce strict public-only resolution checks.",
tags: [
"security",
"access",
"advanced"
]
},
"browser.ssrfPolicy.allowedHostnames": {
label: "Browser Allowed Hostnames",
help: "Explicit hostname allowlist exceptions for SSRF policy checks on browser/network requests. Keep this list minimal and review entries regularly to avoid stale broad access.",
tags: ["access"]
},
"browser.ssrfPolicy.hostnameAllowlist": {
label: "Browser Hostname Allowlist",
help: "Legacy/alternate hostname allowlist field used by SSRF policy consumers for explicit host exceptions. Use stable exact hostnames and avoid wildcard-like broad patterns.",
tags: ["access"]
},
"browser.remoteCdpTimeoutMs": {
label: "Remote CDP Timeout (ms)",
help: "Timeout in milliseconds for connecting to a remote CDP endpoint before failing the browser attach attempt. Increase for high-latency tunnels, or lower for faster failure detection.",
tags: ["performance"]
},
"browser.remoteCdpHandshakeTimeoutMs": {
label: "Remote CDP Handshake Timeout (ms)",
help: "Timeout in milliseconds for post-connect CDP handshake readiness checks against remote browser targets. Raise this for slow-start remote browsers and lower to fail fast in automation loops.",
tags: ["performance"]
},
"session.scope": {
label: "Session Scope",
help: "Sets base session grouping strategy: \"per-sender\" isolates by sender and \"global\" shares one session per channel context. Keep \"per-sender\" for safer multi-user behavior unless deliberate shared context is required.",
tags: ["storage"]
},
"session.dmScope": {
label: "DM Session Scope",
help: "DM session scoping: \"main\" keeps continuity, while \"per-peer\", \"per-channel-peer\", and \"per-account-channel-peer\" increase isolation. Use isolated modes for shared inboxes or multi-account deployments.",
tags: ["storage"]
},
"session.identityLinks": {
label: "Session Identity Links",
help: "Maps canonical identities to provider-prefixed peer IDs so equivalent users resolve to one DM thread (example: telegram:123456). Use this when the same human appears across multiple channels or accounts.",
tags: ["storage"]
},
"session.resetTriggers": {
label: "Session Reset Triggers",
help: "Lists message triggers that force a session reset when matched in inbound content. Use sparingly for explicit reset phrases so context is not dropped unexpectedly during normal conversation.",
tags: ["storage"]
},
"session.idleMinutes": {
label: "Session Idle Minutes",
help: "Applies a legacy idle reset window in minutes for session reuse behavior across inactivity gaps. Use this only for compatibility and prefer structured reset policies under session.reset/session.resetByType.",
tags: ["storage"]
},
"session.reset": {
label: "Session Reset Policy",
help: "Defines the default reset policy object used when no type-specific or channel-specific override applies. Set this first, then layer resetByType or resetByChannel only where behavior must differ.",
tags: ["storage"]
},
"session.reset.mode": {
label: "Session Reset Mode",
help: "Selects reset strategy: \"daily\" resets at a configured hour and \"idle\" resets after inactivity windows. Keep one clear mode per policy to avoid surprising context turnover patterns.",
tags: ["storage"]
},
"session.reset.atHour": {
label: "Session Daily Reset Hour",
help: "Sets local-hour boundary (0-23) for daily reset mode so sessions roll over at predictable times. Use with mode=daily and align to operator timezone expectations for human-readable behavior.",
tags: ["storage"]
},
"session.reset.idleMinutes": {
label: "Session Reset Idle Minutes",
help: "Sets inactivity window before reset for idle mode and can also act as secondary guard with daily mode. Use larger values to preserve continuity or smaller values for fresher short-lived threads.",
tags: ["storage"]
},
"session.resetByType": {
label: "Session Reset by Chat Type",
help: "Overrides reset behavior by chat type (direct, group, thread) when defaults are not sufficient. Use this when group/thread traffic needs different reset cadence than direct messages.",
tags: ["storage"]
},
"session.resetByType.direct": {
label: "Session Reset (Direct)",
help: "Defines reset policy for direct chats and supersedes the base session.reset configuration for that type. Use this as the canonical direct-message override instead of the legacy dm alias.",
tags: ["storage"]
},
"session.resetByType.dm": {
label: "Session Reset (DM Deprecated Alias)",
help: "Deprecated alias for direct reset behavior kept for backward compatibility with older configs. Use session.resetByType.direct instead so future tooling and validation remain consistent.",
tags: ["storage"]
},
"session.resetByType.group": {
label: "Session Reset (Group)",
help: "Defines reset policy for group chat sessions where continuity and noise patterns differ from DMs. Use shorter idle windows for busy groups if context drift becomes a problem.",
tags: ["storage"]
},
"session.resetByType.thread": {
label: "Session Reset (Thread)",
help: "Defines reset policy for thread-scoped sessions, including focused channel thread workflows. Use this when thread sessions should expire faster or slower than other chat types.",
tags: ["storage"]
},
"session.resetByChannel": {
label: "Session Reset by Channel",
help: "Provides channel-specific reset overrides keyed by provider/channel id for fine-grained behavior control. Use this only when one channel needs exceptional reset behavior beyond type-level policies.",
tags: ["storage"]
},
"session.store": {
label: "Session Store Path",
help: "Sets the session storage file path used to persist session records across restarts. Use an explicit path only when you need custom disk layout, backup routing, or mounted-volume storage.",
tags: ["storage"]
},
"session.typingIntervalSeconds": {
label: "Session Typing Interval (seconds)",
help: "Controls interval for repeated typing indicators while replies are being prepared in typing-capable channels. Increase to reduce chatty updates or decrease for more active typing feedback.",
tags: ["performance", "storage"]
},
"session.typingMode": {
label: "Session Typing Mode",
help: "Controls typing behavior timing: \"never\", \"instant\", \"thinking\", or \"message\" based emission points. Keep conservative modes in high-volume channels to avoid unnecessary typing noise.",
tags: ["storage"]
},
"session.parentForkMaxTokens": {
label: "Session Parent Fork Max Tokens",
help: "Maximum parent-session token count allowed for thread/session inheritance forking. If the parent exceeds this, OpenClaw starts a fresh thread session instead of forking; set 0 to disable this protection.",
tags: [
"security",
"auth",
"performance",
"storage"
]
},
"session.mainKey": {
label: "Session Main Key",
help: "Overrides the canonical main session key used for continuity when dmScope or routing logic points to \"main\". Use a stable value only if you intentionally need custom session anchoring.",
tags: ["storage"]
},
"session.sendPolicy": {
label: "Session Send Policy",
help: "Controls cross-session send permissions using allow/deny rules evaluated against channel, chatType, and key prefixes. Use this to fence where session tools can deliver messages in complex environments.",
tags: ["access", "storage"]
},
"session.sendPolicy.default": {
label: "Session Send Policy Default Action",
help: "Sets fallback action when no sendPolicy rule matches: \"allow\" or \"deny\". Keep \"allow\" for simpler setups, or choose \"deny\" when you require explicit allow rules for every destination.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules": {
label: "Session Send Policy Rules",
help: "Ordered allow/deny rules evaluated before the default action, for example `{ action: \"deny\", match: { channel: \"discord\" } }`. Put most specific rules first so broad rules do not shadow exceptions.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules[].action": {
label: "Session Send Rule Action",
help: "Defines rule decision as \"allow\" or \"deny\" when the corresponding match criteria are satisfied. Use deny-first ordering when enforcing strict boundaries with explicit allow exceptions.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules[].match": {
label: "Session Send Rule Match",
help: "Defines optional rule match conditions that can combine channel, chatType, and key-prefix constraints. Keep matches narrow so policy intent stays readable and debugging remains straightforward.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules[].match.channel": {
label: "Session Send Rule Channel",
help: "Matches rule application to a specific channel/provider id (for example discord, telegram, slack). Use this when one channel should permit or deny delivery independently of others.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules[].match.chatType": {
label: "Session Send Rule Chat Type",
help: "Matches rule application to chat type (direct, group, thread) so behavior varies by conversation form. Use this when DM and group destinations require different safety boundaries.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules[].match.keyPrefix": {
label: "Session Send Rule Key Prefix",
help: "Matches a normalized session-key prefix after internal key normalization steps in policy consumers. Use this for general prefix controls, and prefer rawKeyPrefix when exact full-key matching is required.",
tags: ["access", "storage"]
},
"session.sendPolicy.rules[].match.rawKeyPrefix": {
label: "Session Send Rule Raw Key Prefix",
help: "Matches the raw, unnormalized session-key prefix for exact full-key policy targeting. Use this when normalized keyPrefix is too broad and you need agent-prefixed or transport-specific precision.",
tags: ["access", "storage"]
},
"session.agentToAgent": {
label: "Session Agent-to-Agent",
help: "Groups controls for inter-agent session exchanges, including loop prevention limits on reply chaining. Keep defaults unless you run advanced agent-to-agent automation with strict turn caps.",
tags: ["storage"]
},
"session.agentToAgent.maxPingPongTurns": {
label: "Agent-to-Agent Ping-Pong Turns",
help: "Max reply-back turns between requester and target agents during agent-to-agent exchanges (0-5). Use lower values to hard-limit chatter loops and preserve predictable run completion.",
tags: ["performance", "storage"]
},
"session.threadBindings": {
label: "Session Thread Bindings",
help: "Shared defaults for thread-bound session routing behavior across providers that support thread focus workflows. Configure global defaults here and override per channel only when behavior differs.",
tags: ["storage"]
},
"session.threadBindings.enabled": {
label: "Thread Binding Enabled",
help: "Global master switch for thread-bound session routing features and focused thread delivery behavior. Keep enabled for modern thread workflows unless you need to disable thread binding globally.",
tags: ["storage"]
},
"session.threadBindings.idleHours": {
label: "Thread Binding Idle Timeout (hours)",
help: "Default inactivity window in hours for thread-bound sessions across providers/channels (0 disables idle auto-unfocus). Default: 24.",
tags: ["storage"]
},
"session.threadBindings.maxAgeHours": {
label: "Thread Binding Max Age (hours)",
help: "Optional hard max age in hours for thread-bound sessions across providers/channels (0 disables hard cap). Default: 0.",
tags: ["performance", "storage"]
},
"session.maintenance": {
label: "Session Maintenance",
help: "Automatic session-store maintenance controls for pruning age, entry caps, and file rotation behavior. Start in warn mode to observe impact, then enforce once thresholds are tuned.",
tags: ["storage"]
},
"session.maintenance.mode": {
label: "Session Maintenance Mode",
help: "Determines whether maintenance policies are only reported (\"warn\") or actively applied (\"enforce\"). Keep \"warn\" during rollout and switch to \"enforce\" after validating safe thresholds.",
tags: ["storage"]
},
"session.maintenance.pruneAfter": {
label: "Session Prune After",
help: "Removes entries older than this duration (for example `30d` or `12h`) during maintenance passes. Use this as the primary age-retention control and align it with data retention policy.",
tags: ["storage"]
},
"session.maintenance.pruneDays": {
label: "Session Prune Days (Deprecated)",
help: "Deprecated age-retention field kept for compatibility with legacy configs using day counts. Use session.maintenance.pruneAfter instead so duration syntax and behavior are consistent.",
tags: ["storage"]
},
"session.maintenance.maxEntries": {
label: "Session Max Entries",
help: "Caps total session entry count retained in the store to prevent unbounded growth over time. Use lower limits for constrained environments, or higher limits when longer history is required.",
tags: ["performance", "storage"]
},
"session.maintenance.rotateBytes": {
label: "Session Rotate Size",
help: "Rotates the session store when file size exceeds a threshold such as `10mb` or `1gb`. Use this to bound single-file growth and keep backup/restore operations manageable.",
tags: ["storage"]
},
"session.maintenance.resetArchiveRetention": {
label: "Session Reset Archive Retention",
help: "Retention for reset transcript archives (`*.reset.<timestamp>`). Accepts a duration (for example `30d`), or `false` to disable cleanup. Defaults to pruneAfter so reset artifacts do not grow forever.",
tags: ["storage"]
},
"session.maintenance.maxDiskBytes": {
label: "Session Max Disk Budget",
help: "Optional per-agent sessions-directory disk budget (for example `500mb`). Use this to cap session storage per agent; when exceeded, warn mode reports pressure and enforce mode performs oldest-first cleanup.",
tags: ["performance", "storage"]
},
"session.maintenance.highWaterBytes": {
label: "Session Disk High-water Target",
help: "Target size after disk-budget cleanup (high-water mark). Defaults to 80% of maxDiskBytes; set explicitly for tighter reclaim behavior on constrained disks.",
tags: ["storage"]
},
"cron.enabled": {
label: "Cron Enabled",
help: "Enables cron job execution for stored schedules managed by the gateway. Keep enabled for normal reminder/automation flows, and disable only to pause all cron execution without deleting jobs.",
tags: ["automation"]
},
"cron.store": {
label: "Cron Store Path",
help: "Path to the cron job store file used to persist scheduled jobs across restarts. Set an explicit path only when you need custom storage layout, backups, or mounted volumes.",
tags: ["storage", "automation"]
},
"cron.maxConcurrentRuns": {
label: "Cron Max Concurrent Runs",
help: "Limits how many cron jobs can execute at the same time when multiple schedules fire together. Use lower values to protect CPU/memory under heavy automation load, or raise carefully for higher throughput.",
tags: ["performance", "automation"]
},
"cron.retry": {
label: "Cron Retry Policy",
help: "Overrides the default retry policy for one-shot jobs when they fail with transient errors (rate limit, overloaded, network, server_error). Omit to use defaults: maxAttempts 3, backoffMs [30000, 60000, 300000], retry all transient types.",
tags: ["reliability", "automation"]
},
"cron.retry.maxAttempts": {
label: "Cron Retry Max Attempts",
help: "Max retries for one-shot jobs on transient errors before permanent disable (default: 3).",
tags: [
"reliability",
"performance",
"automation"
]
},
"cron.retry.backoffMs": {
label: "Cron Retry Backoff (ms)",
help: "Backoff delays in ms for each retry attempt (default: [30000, 60000, 300000]). Use shorter values for faster retries.",
tags: ["reliability", "automation"]
},
"cron.retry.retryOn": {
label: "Cron Retry Error Types",
help: "Error types to retry: rate_limit, overloaded, network, timeout, server_error. Use to restrict which errors trigger retries; omit to retry all transient types.",
tags: ["reliability", "automation"]
},
"cron.webhook": {
label: "Cron Legacy Webhook (Deprecated)",
help: "Deprecated legacy fallback webhook URL used only for old jobs with `notify=true`. Migrate to per-job delivery using `delivery.mode=\"webhook\"` plus `delivery.to`, and avoid relying on this global field.",
tags: ["automation"]
},
"cron.webhookToken": {
label: "Cron Webhook Bearer Token",
help: "Bearer token attached to cron webhook POST deliveries when webhook mode is used. Prefer secret/env substitution and rotate this token regularly if shared webhook endpoints are internet-reachable.",
tags: [
"security",
"auth",
"automation"
],
sensitive: true
},
"cron.sessionRetention": {
label: "Cron Session Retention",
help: "Controls how long completed cron run sessions are kept before pruning (`24h`, `7d`, `1h30m`, or `false` to disable pruning; default: `24h`). Use shorter retention to reduce storage growth on high-frequency schedules.",
tags: ["storage", "automation"]
},
"cron.runLog": {
label: "Cron Run Log Pruning",
help: "Pruning controls for per-job cron run history files under `cron/runs/<jobId>.jsonl`, including size and line retention.",
tags: ["automation"]
},
"cron.runLog.maxBytes": {
label: "Cron Run Log Max Bytes",
help: "Maximum bytes per cron run-log file before pruning rewrites to the last keepLines entries (for example `2mb`, default `2000000`).",
tags: ["performance", "automation"]
},
"cron.runLog.keepLines": {
label: "Cron Run Log Keep Lines",
help: "How many trailing run-log lines to retain when a file exceeds maxBytes (default `2000`). Increase for longer forensic history or lower for smaller disks.",
tags: ["automation"]
},
"hooks.enabled": {
label: "Hooks Enabled",
help: "Enables the hooks endpoint and mapping execution pipeline for inbound webhook requests. Keep disabled unless you are actively routing external events into the gateway.",
tags: ["advanced"]
},
"hooks.path": {
label: "Hooks Endpoint Path",
help: "HTTP path used by the hooks endpoint (for example `/hooks`) on the gateway control server. Use a non-guessable path and combine it with token validation for defense in depth.",
tags: ["storage"]
},
"hooks.token": {
label: "Hooks Auth Token",
help: "Shared bearer token checked by hooks ingress for request authentication before mappings run. Treat holders as full-trust callers for the hook ingress surface, not as a separate non-owner role. Use environment substitution and rotate regularly when webhook endpoints are internet-accessible.",
tags: ["security", "auth"],
sensitive: true
},
"hooks.defaultSessionKey": {
label: "Hooks Default Session Key",
help: "Fallback session key used for hook deliveries when a request does not provide one through allowed channels. Use a stable but scoped key to avoid mixing unrelated automation conversations.",
tags: ["storage"]
},
"hooks.allowRequestSessionKey": {
label: "Hooks Allow Request Session Key",
help: "Allows callers to supply a session key in hook requests when true, enabling caller-controlled routing. Keep false unless trusted integrators explicitly need custom session threading.",
tags: ["access", "storage"]
},
"hooks.allowedSessionKeyPrefixes": {
label: "Hooks Allowed Session Key Prefixes",
help: "Allowlist of accepted session-key prefixes for inbound hook requests when caller-provided keys are enabled. Use narrow prefixes to prevent arbitrary session-key injection.",
tags: ["access", "storage"]
},
"hooks.allowedAgentIds": {
label: "Hooks Allowed Agent IDs",
help: "Allowlist of agent IDs that hook mappings are allowed to target when selecting execution agents. Use this to constrain automation events to dedicated service agents and reduce blast radius if a hook token is exposed.",
tags: ["access"]
},
"hooks.maxBodyBytes": {
label: "Hooks Max Body Bytes",
help: "Maximum accepted webhook payload size in bytes before the request is rejected. Keep this bounded to reduce abuse risk and protect memory usage under bursty integrations.",
tags: ["performance"]
},
"hooks.presets": {
label: "Hooks Presets",
help: "Named hook preset bundles applied at load time to seed standard mappings and behavior defaults. Keep preset usage explicit so operators can audit which automations are active.",
tags: ["advanced"]
},
"hooks.transformsDir": {
label: "Hooks Transforms Directory",
help: "Base directory for hook transform modules referenced by mapping transform.module paths. Use a controlled repo directory so dynamic imports remain reviewable and predictable.",
tags: ["storage"]
},
"hooks.mappings": {
label: "Hook Mappings",
help: "Ordered mapping rules that match inbound hook requests and choose wake or agent actions with optional delivery routing. Use specific mappings first to avoid broad pattern rules capturing everything.",
tags: ["advanced"]
},
"hooks.mappings[].id": {
label: "Hook Mapping ID",
help: "Optional stable identifier for a hook mapping entry used for auditing, troubleshooting, and targeted updates. Use unique IDs so logs and config diffs can reference mappings unambiguously.",
tags: ["advanced"]
},
"hooks.mappings[].match": {
label: "Hook Mapping Match",
help: "Grouping object for mapping match predicates such as path and source before action routing is applied. Keep match criteria specific so unrelated webhook traffic does not trigger automations.",
tags: ["advanced"]
},
"hooks.mappings[].match.path": {
label: "Hook Mapping Match Path",
help: "Path match condition for a hook mapping, usually compared against the inbound request path. Use this to split automation behavior by webhook endpoint path families.",
tags: ["storage"]
},
"hooks.mappings[].match.source": {
label: "Hook Mapping Match Source",
help: "Source match condition for a hook mapping, typically set by trusted upstream metadata or adapter logic. Use stable source identifiers so routing remains deterministic across retries.",
tags: ["advanced"]
},
"hooks.mappings[].action": {
label: "Hook Mapping Action",
help: "Mapping action type: \"wake\" triggers agent wake flow, while \"agent\" sends directly to agent handling. Use \"agent\" for immediate execution and \"wake\" when heartbeat-driven processing is preferred.",
tags: ["advanced"]
},
"hooks.mappings[].wakeMode": {
label: "Hook Mapping Wake Mode",
help: "Wake scheduling mode: \"now\" wakes immediately, while \"next-heartbeat\" defers until the next heartbeat cycle. Use deferred mode for lower-priority automations that can tolerate slight delay.",
tags: ["advanced"]
},
"hooks.mappings[].name": {
label: "Hook Mapping Name",
help: "Human-readable mapping display name used in diagnostics and operator-facing config UIs. Keep names concise and descriptive so routing intent is obvious during incident review.",
tags: ["advanced"]
},
"hooks.mappings[].agentId": {
label: "Hook Mapping Agent ID",
help: "Target agent ID for mapping execution when action routing should not use defaults. Use dedicated automation agents to isolate webhook behavior from interactive operator sessions.",
tags: ["advanced"]
},
"hooks.mappings[].sessionKey": {
label: "Hook Mapping Session Key",
help: "Explicit session key override for mapping-delivered messages to control thread continuity. Use stable scoped keys so repeated events correlate without leaking into unrelated conversations.",
tags: ["security", "storage"],
sensitive: true
},
"hooks.mappings[].messageTemplate": {
label: "Hook Mapping Message Template",
help: "Template for synthesizing structured mapping input into the final message content sent to the target action path. Keep templates deterministic so downstream parsing and behavior remain stable.",
tags: ["advanced"]
},
"hooks.mappings[].textTemplate": {
label: "Hook Mapping Text Template",
help: "Text-only fallback template used when rich payload rendering is not desired or not supported. Use this to provide a concise, consistent summary string for chat delivery surfaces.",
tags: ["advanced"]
},
"hooks.mappings[].deliver": {
label: "Hook Mapping Deliver Reply",
help: "Controls whether mapping execution results are delivered back to a channel destination versus being processed silently. Disable delivery for background automations that should not post user-facing output.",
tags: ["advanced"]
},
"hooks.mappings[].allowUnsafeExternalContent": {
label: "Hook Mapping Allow Unsafe External Content",
help: "When true, mapping content may include less-sanitized external payload data in generated messages. Keep false by default and enable only for trusted sources with reviewed transform logic.",
tags: ["access"]
},
"hooks.mappings[].channel": {
label: "Hook Mapping Delivery Channel",
help: "Delivery channel override for mapping outputs (for example \"last\", \"telegram\", \"discord\", \"slack\", \"signal\", \"imessage\", or \"msteams\"). Keep channel overrides explicit to avoid accidental cross-channel sends.",
tags: ["advanced"]
},
"hooks.mappings[].to": {
label: "Hook Mapping Delivery Destination",
help: "Destination identifier inside the selected channel when mapping replies should route to a fixed target. Verify provider-specific destination formats before enabling production mappings.",
tags: ["advanced"]
},
"hooks.mappings[].model": {
label: "Hook Mapping Model Override",
help: "Optional model override for mapping-triggered runs when automation should use a different model than agent defaults. Use this sparingly so behavior remains predictable across mapping executions.",
tags: ["models"]
},
"hooks.mappings[].thinking": {
label: "Hook Mapping Thinking Override",
help: "Optional thinking-effort override for mapping-triggered runs to tune latency versus reasoning depth. Keep low or minimal for high-volume hooks unless deeper reasoning is clearly required.",
tags: ["advanced"]
},
"hooks.mappings[].timeoutSeconds": {
label: "Hook Mapping Timeout (sec)",
help: "Maximum runtime allowed for mapping action execution before timeout handling applies. Use tighter limits for high-volume webhook sources to prevent queue pileups.",
tags: ["performance"]
},
"hooks.mappings[].transform": {
label: "Hook Mapping Transform",
help: "Transform configuration block defining module/export preprocessing before mapping action handling. Use transforms only from reviewed code paths and keep behavior deterministic for repeatable automation.",
tags: ["advanced"]
},
"hooks.mappings[].transform.module": {
label: "Hook Transform Module",
help: "Relative transform module path loaded from hooks.transformsDir to rewrite incoming payloads before delivery. Keep modules local, reviewed, and free of path traversal patterns.",
tags: ["advanced"]
},
"hooks.mappings[].transform.export": {
label: "Hook Transform Export",
help: "Named export to invoke from the transform module; defaults to module default export when omitted. Set this when one file hosts multiple transform handlers.",
tags: ["advanced"]
},
"hooks.gmail": {
label: "Gmail Hook",
help: "Gmail push integration settings used for Pub/Sub notifications and optional local callback serving. Keep this scoped to dedicated Gmail automation accounts where possible.",
tags: ["advanced"]
},
"hooks.gmail.account": {
label: "Gmail Hook Account",
help: "Google account identifier used for Gmail watch/subscription operations in this hook integration. Use a dedicated automation mailbox account to isolate operational permissions.",
tags: ["advanced"]
},
"hooks.gmail.label": {
label: "Gmail Hook Label",
help: "Optional Gmail label filter limiting which labeled messages trigger hook events. Keep filters narrow to avoid flooding automations with unrelated inbox traffic.",
tags: ["advanced"]
},
"hooks.gmail.topic": {
label: "Gmail Hook Pub/Sub Topic",
help: "Google Pub/Sub topic name used by Gmail watch to publish change notifications for this account. Ensure the topic IAM grants Gmail publish access before enabling watches.",
tags: ["advanced"]
},
"hooks.gmail.subscription": {
label: "Gmail Hook Subscription",
help: "Pub/Sub subscription consumed by the gateway to receive Gmail change notifications from the configured topic. Keep subscription ownership clear so multiple consumers do not race unexpectedly.",
tags: ["advanced"]
},
"hooks.gmail.pushToken": {
label: "Gmail Hook Push Token",
help: "Shared secret token required on Gmail push hook callbacks before processing notifications. Use env substitution and rotate if callback endpoints are exposed externally.",
tags: ["security", "auth"],
sensitive: true
},
"hooks.gmail.hookUrl": {
label: "Gmail Hook Callback URL",
help: "Public callback URL Gmail or intermediaries invoke to deliver notifications into this hook pipeline. Keep this URL protected with token validation and restricted network exposure.",
tags: ["advanced"]
},
"hooks.gmail.includeBody": {
label: "Gmail Hook Include Body",
help: "When true, fetch and include email body content for downstream mapping/agent processing. Keep false unless body text is required, because this increases payload size and sensitivity.",
tags: ["advanced"]
},
"hooks.gmail.maxBytes": {
label: "Gmail Hook Max Body Bytes",
help: "Maximum Gmail payload bytes processed per event when includeBody is enabled. Keep conservative limits to reduce oversized message processing cost and risk.",
tags: ["performance"]
},
"hooks.gmail.renewEveryMinutes": {
label: "Gmail Hook Renew Interval (min)",
help: "Renewal cadence in minutes for Gmail watch subscriptions to prevent expiration. Set below provider expiration windows and monitor renew failures in logs.",
tags: ["advanced"]
},
"hooks.gmail.allowUnsafeExternalContent": {
label: "Gmail Hook Allow Unsafe External Content",
help: "Allows less-sanitized external Gmail content to pass into processing when enabled. Keep disabled for safer defaults, and enable only for trusted mail streams with controlled transforms.",
tags: ["access"]
},
"hooks.gmail.serve": {
label: "Gmail Hook Local Server",
help: "Local callback server settings block for directly receiving Gmail notifications without a separate ingress layer. Enable only when this process should terminate webhook traffic itself.",
tags: ["advanced"]
},
"hooks.gmail.serve.bind": {
label: "Gmail Hook Server Bind Address",
help: "Bind address for the local Gmail callback HTTP server used when serving hooks directly. Keep loopback-only unless external ingress is intentionally required.",
tags: ["advanced"]
},
"hooks.gmail.serve.port": {
label: "Gmail Hook Server Port",
help: "Port for the local Gmail callback HTTP server when serve mode is enabled. Use a dedicated port to avoid collisions with gateway/control interfaces.",
tags: ["advanced"]
},
"hooks.gmail.serve.path": {
label: "Gmail Hook Server Path",
help: "HTTP path on the local Gmail callback server where push notifications are accepted. Keep this consistent with subscription configuration to avoid dropped events.",
tags: ["storage"]
},
"hooks.gmail.tailscale": {
label: "Gmail Hook Tailscale",
help: "Tailscale exposure configuration block for publishing Gmail callbacks through Serve/Funnel routes. Use private tailnet modes before enabling any public ingress path.",
tags: ["advanced"]
},
"hooks.gmail.tailscale.mode": {
label: "Gmail Hook Tailscale Mode",
help: "Tailscale exposure mode for Gmail callbacks: \"off\", \"serve\", or \"funnel\". Use \"serve\" for private tailnet delivery and \"funnel\" only when public internet ingress is required.",
tags: ["advanced"]
},
"hooks.gmail.tailscale.path": {
label: "Gmail Hook Tailscale Path",
help: "Path published by Tailscale Serve/Funnel for Gmail callback forwarding when enabled. Keep it aligned with Gmail webhook config so requests reach the expected handler.",
tags: ["storage"]
},
"hooks.gmail.tailscale.target": {
label: "Gmail Hook Tailscale Target",
help: "Local service target forwarded by Tailscale Serve/Funnel (for example http://127.0.0.1:8787). Use explicit loopback targets to avoid ambiguous routing.",
tags: ["advanced"]
},
"hooks.gmail.model": {
label: "Gmail Hook Model Override",
help: "Optional model override for Gmail-triggered runs when mailbox automations should use dedicated model behavior. Keep unset to inherit agent defaults unless mailbox tasks need specialization.",
tags: ["models"]
},
"hooks.gmail.thinking": {
label: "Gmail Hook Thinking Override",
help: "Thinking effort override for Gmail-driven agent runs: \"off\", \"minimal\", \"low\", \"medium\", or \"high\". Keep modest defaults for routine inbox automations to control cost and latency.",
tags: ["advanced"]
},
"hooks.internal": {
label: "Internal Hooks",
help: "Internal hook runtime settings for bundled/custom event handlers loaded from module paths. Use this for trusted in-process automations and keep handler loading tightly scoped.",
tags: ["advanced"]
},
"hooks.internal.enabled": {
label: "Internal Hooks Enabled",
help: "Enables processing for internal hook handlers and configured entries in the internal hook runtime. Keep disabled unless internal hook handlers are intentionally configured.",
tags: ["advanced"]
},
"hooks.internal.handlers": {
label: "Internal Hook Handlers",
help: "List of internal event handlers mapping event names to modules and optional exports. Keep handler definitions explicit so event-to-code routing is auditable.",
tags: ["advanced"]
},
"hooks.internal.handlers[].event": {
label: "Internal Hook Event",
help: "Internal event name that triggers this handler module when emitted by the runtime. Use stable event naming conventions to avoid accidental overlap across handlers.",
tags: ["advanced"]
},
"hooks.internal.handlers[].module": {
label: "Internal Hook Module",
help: "Safe relative module path for the internal hook handler implementation loaded at runtime. Keep module files in reviewed directories and avoid dynamic path composition.",
tags: ["advanced"]
},
"hooks.internal.handlers[].export": {
label: "Internal Hook Export",
help: "Optional named export for the internal hook handler function when module default export is not used. Set this when one module ships multiple handler entrypoints.",
tags: ["advanced"]
},
"hooks.internal.entries": {
label: "Internal Hook Entries",
help: "Configured internal hook entry records used to register concrete runtime handlers and metadata. Keep entries explicit and versioned so production behavior is auditable.",
tags: ["advanced"]
},
"hooks.internal.load": {
label: "Internal Hook Loader",
help: "Internal hook loader settings controlling where handler modules are discovered at startup. Use constrained load roots to reduce accidental module conflicts or shadowing.",
tags: ["advanced"]
},
"hooks.internal.load.extraDirs": {
label: "Internal Hook Extra Directories",
help: "Additional directories searched for internal hook modules beyond default load paths. Keep this minimal and controlled to reduce accidental module shadowing.",
tags: ["storage"]
},
"hooks.internal.installs": {
label: "Internal Hook Install Records",
help: "Install metadata for internal hook modules, including source and resolved artifacts for repeatable deployments. Use this as operational provenance and avoid manual drift edits.",
tags: ["advanced"]
},
web: {
label: "Web Channel",
help: "Web channel runtime settings for heartbeat and reconnect behavior when operating web-based chat surfaces. Use reconnect values tuned to your network reliability profile and expected uptime needs.",
tags: ["advanced"]
},
"web.enabled": {
label: "Web Channel Enabled",
help: "Enables the web channel runtime and related websocket lifecycle behavior. Keep disabled when web chat is unused to reduce active connection management overhead.",
tags: ["advanced"]
},
"web.heartbeatSeconds": {
label: "Web Channel Heartbeat Interval (sec)",
help: "Heartbeat interval in seconds for web channel connectivity and liveness maintenance. Use shorter intervals for faster detection, or longer intervals to reduce keepalive chatter.",
tags: ["automation"]
},
"web.reconnect": {
label: "Web Channel Reconnect Policy",
help: "Reconnect backoff policy for web channel reconnect attempts after transport failure. Keep bounded retries and jitter tuned to avoid thundering-herd reconnect behavior.",
tags: ["advanced"]
},
"web.reconnect.initialMs": {
label: "Web Reconnect Initial Delay (ms)",
help: "Initial reconnect delay in milliseconds before the first retry after disconnection. Use modest delays to recover quickly without immediate retry storms.",
tags: ["advanced"]
},
"web.reconnect.maxMs": {
label: "Web Reconnect Max Delay (ms)",
help: "Maximum reconnect backoff cap in milliseconds to bound retry delay growth over repeated failures. Use a reasonable cap so recovery remains timely after prolonged outages.",
tags: ["performance"]
},
"web.reconnect.factor": {
label: "Web Reconnect Backoff Factor",
help: "Exponential backoff multiplier used between reconnect attempts in web channel retry loops. Keep factor above 1 and tune with jitter for stable large-fleet reconnect behavior.",
tags: ["advanced"]
},
"web.reconnect.jitter": {
label: "Web Reconnect Jitter",
help: "Randomization factor (0-1) applied to reconnect delays to desynchronize clients after outage events. Keep non-zero jitter in multi-client deployments to reduce synchronized spikes.",
tags: ["advanced"]
},
"web.reconnect.maxAttempts": {
label: "Web Reconnect Max Attempts",
help: "Maximum reconnect attempts before giving up for the current failure sequence (0 means no retries). Use finite caps for controlled failure handling in automation-sensitive environments.",
tags: ["performance"]
},
"discovery.wideArea": {
label: "Wide-area Discovery",
help: "Wide-area discovery configuration group for exposing discovery signals beyond local-link scopes. Enable only in deployments that intentionally aggregate gateway presence across sites.",
tags: ["network"]
},
"discovery.wideArea.enabled": {
label: "Wide-area Discovery Enabled",
help: "Enables wide-area discovery signaling when your environment needs non-local gateway discovery. Keep disabled unless cross-network discovery is operationally required.",
tags: ["network"]
},
"discovery.wideArea.domain": {
label: "Wide-area Discovery Domain",
help: "Optional unicast DNS-SD domain for wide-area discovery, such as openclaw.internal. Use this when you intentionally publish gateway discovery beyond local mDNS scopes.",
tags: ["network"]
},
"discovery.mdns": {
label: "mDNS Discovery",
help: "mDNS discovery configuration group for local network advertisement and discovery behavior tuning. Keep minimal mode for routine LAN discovery unless extra metadata is required.",
tags: ["network"]
},
canvasHost: {
label: "Canvas Host",
help: "Canvas host settings for serving canvas assets and local live-reload behavior used by canvas-enabled workflows. Keep disabled unless canvas-hosted assets are actively used.",
tags: ["advanced"]
},
"canvasHost.enabled": {
label: "Canvas Host Enabled",
help: "Enables the canvas host server process and routes for serving canvas files. Keep disabled when canvas workflows are inactive to reduce exposed local services.",
tags: ["advanced"]
},
"canvasHost.root": {
label: "Canvas Host Root Directory",
help: "Filesystem root directory served by canvas host for canvas content and static assets. Use a dedicated directory and avoid broad repo roots for least-privilege file exposure.",
tags: ["advanced"]
},
"canvasHost.port": {
label: "Canvas Host Port",
help: "TCP port used by the canvas host HTTP server when canvas hosting is enabled. Choose a non-conflicting port and align firewall/proxy policy accordingly.",
tags: ["advanced"]
},
"canvasHost.liveReload": {
label: "Canvas Host Live Reload",
help: "Enables automatic live-reload behavior for canvas assets during development workflows. Keep disabled in production-like environments where deterministic output is preferred.",
tags: ["reliability"]
},
"talk.voiceId": {
label: "Talk Voice ID",
help: "Legacy ElevenLabs default voice ID for Talk mode. Prefer talk.providers.elevenlabs.voiceId.",
tags: ["media"]
},
"talk.voiceAliases": {
label: "Talk Voice Aliases",
help: "Use this legacy ElevenLabs voice alias map (for example {\"Clawd\":\"EXAVITQu4vr4xnSDxMaL\"}) only during migration. Prefer talk.providers.elevenlabs.voiceAliases.",
tags: ["media"]
},
"talk.modelId": {
label: "Talk Model ID",
help: "Legacy ElevenLabs model ID for Talk mode (default: eleven_v3). Prefer talk.providers.elevenlabs.modelId.",
tags: ["models", "media"]
},
"talk.outputFormat": {
label: "Talk Output Format",
help: "Use this legacy ElevenLabs output format for Talk mode (for example pcm_44100 or mp3_44100_128) only during migration. Prefer talk.providers.elevenlabs.outputFormat.",
tags: ["media"]
},
"talk.interruptOnSpeech": {
label: "Talk Interrupt on Speech",
help: "If true (default), stop assistant speech when the user starts speaking in Talk mode. Keep enabled for conversational turn-taking.",
tags: ["media"]
},
"talk.silenceTimeoutMs": {
label: "Talk Silence Timeout (ms)",
help: "Milliseconds of user silence before Talk mode finalizes and sends the current transcript. Leave unset to keep the platform default pause window (700 ms on macOS and Android, 900 ms on iOS).",
tags: ["performance", "media"]
},
"messages.messagePrefix": {
label: "Inbound Message Prefix",
help: "Prefix text prepended to inbound user messages before they are handed to the agent runtime. Use this sparingly for channel context markers and keep it stable across sessions.",
tags: ["advanced"]
},
"messages.responsePrefix": {
label: "Outbound Response Prefix",
help: "Prefix text prepended to outbound assistant replies before sending to channels. Use for lightweight branding/context tags and avoid long prefixes that reduce content density.",
tags: ["advanced"]
},
"messages.groupChat": {
label: "Group Chat Rules",
help: "Group-message handling controls including mention triggers and history window sizing. Keep mention patterns narrow so group channels do not trigger on every message.",
tags: ["advanced"]
},
"messages.groupChat.mentionPatterns": {
label: "Group Mention Patterns",
help: "Safe case-insensitive regex patterns used to detect explicit mentions/trigger phrases in group chats. Use precise patterns to reduce false positives in high-volume channels; invalid or unsafe nested-repetition patterns are ignored.",
tags: ["advanced"]
},
"messages.groupChat.historyLimit": {
label: "Group History Limit",
help: "Maximum number of prior group messages loaded as context per turn for group sessions. Use higher values for richer continuity, or lower values for faster and cheaper responses.",
tags: ["performance"]
},
"messages.queue": {
label: "Inbound Queue",
help: "Inbound message queue strategy used to buffer bursts before processing turns. Tune this for busy channels where sequential processing or batching behavior matters.",
tags: ["advanced"]
},
"messages.queue.mode": {
label: "Queue Mode",
help: "Queue behavior mode: \"steer\", \"followup\", \"collect\", \"steer-backlog\", \"steer+backlog\", \"queue\", or \"interrupt\". Keep conservative modes unless you intentionally need aggressive interruption/backlog semantics.",
tags: ["advanced"]
},
"messages.queue.byChannel": {
label: "Queue Mode by Channel",
help: "Per-channel queue mode overrides keyed by provider id (for example telegram, discord, slack). Use this when one channel’s traffic pattern needs different queue behavior than global defaults.",
tags: ["advanced"]
},
"messages.queue.debounceMs": {
label: "Queue Debounce (ms)",
help: "Global queue debounce window in milliseconds before processing buffered inbound messages. Use higher values to coalesce rapid bursts, or lower values for reduced response latency.",
tags: ["performance"]
},
"messages.queue.debounceMsByChannel": {
label: "Queue Debounce by Channel (ms)",
help: "Per-channel debounce overrides for queue behavior keyed by provider id. Use this to tune burst handling independently for chat surfaces with different pacing.",
tags: ["performance"]
},
"messages.queue.cap": {
label: "Queue Capacity",
help: "Maximum number of queued inbound items retained before drop policy applies. Keep caps bounded in noisy channels so memory usage remains predictable.",
tags: ["advanced"]
},
"messages.queue.drop": {
label: "Queue Drop Strategy",
help: "Drop strategy when queue cap is exceeded: \"old\", \"new\", or \"summarize\". Use summarize when preserving intent matters, or old/new when deterministic dropping is preferred.",
tags: ["advanced"]
},
"messages.inbound": {
label: "Inbound Debounce",
help: "Direct inbound debounce settings used before queue/turn processing starts. Configure this for provider-specific rapid message bursts from the same sender.",
tags: ["advanced"]
},
"messages.suppressToolErrors": {
label: "Suppress Tool Error Warnings",
help: "When true, suppress ⚠️ tool-error warnings from being shown to the user. The agent already sees errors in context and can retry. Default: false.",
tags: ["advanced"]
},
"messages.ackReaction": {
label: "Ack Reaction Emoji",
help: "Emoji reaction used to acknowledge inbound messages (empty disables).",
tags: ["advanced"]
},
"messages.ackReactionScope": {
label: "Ack Reaction Scope",
help: "When to send ack reactions (\"group-mentions\", \"group-all\", \"direct\", \"all\", \"off\", \"none\"). \"off\"/\"none\" disables ack reactions entirely.",
tags: ["advanced"]
},
"messages.removeAckAfterReply": {
label: "Remove Ack Reaction After Reply",
help: "Removes the acknowledgment reaction after final reply delivery when enabled. Keep enabled for cleaner UX in channels where persistent ack reactions create clutter.",
tags: ["advanced"]
},
"messages.statusReactions": {
label: "Status Reactions",
help: "Lifecycle status reactions that update the emoji on the trigger message as the agent progresses (queued → thinking → tool → done/error).",
tags: ["advanced"]
},
"messages.statusReactions.enabled": {
label: "Enable Status Reactions",
help: "Enable lifecycle status reactions for Telegram. When enabled, the ack reaction becomes the initial 'queued' state and progresses through thinking, tool, done/error automatically. Default: false.",
tags: ["advanced"]
},
"messages.statusReactions.emojis": {
label: "Status Reaction Emojis",
help: "Override default status reaction emojis. Keys: thinking, compacting, tool, coding, web, done, error, stallSoft, stallHard. Must be valid Telegram reaction emojis.",
tags: ["advanced"]
},
"messages.statusReactions.timing": {
label: "Status Reaction Timing",
help: "Override default timing. Keys: debounceMs (700), stallSoftMs (25000), stallHardMs (60000), doneHoldMs (1500), errorHoldMs (2500).",
tags: ["advanced"]
},
"messages.inbound.debounceMs": {
label: "Inbound Message Debounce (ms)",
help: "Debounce window (ms) for batching rapid inbound messages from the same sender (0 to disable).",
tags: ["performance"]
},
"messages.inbound.byChannel": {
label: "Inbound Debounce by Channel (ms)",
help: "Per-channel inbound debounce overrides keyed by provider id in milliseconds. Use this where some providers send message fragments more aggressively than others.",
tags: ["advanced"]
},
"messages.tts": {
label: "Message Text-to-Speech",
help: "Text-to-speech policy for reading agent replies aloud on supported voice or audio surfaces. Keep disabled unless voice playback is part of your operator/user workflow.",
tags: ["media"]
},
"talk.provider": {
label: "Talk Active Provider",
help: "Active Talk provider id (for example \"elevenlabs\").",
tags: ["media"]
},
"talk.providers": {
label: "Talk Provider Settings",
help: "Provider-specific Talk settings keyed by provider id. During migration, prefer this over legacy talk.* keys.",
tags: ["media"]
},
"talk.providers.*.voiceId": {
label: "Talk Provider Voice ID",
help: "Provider default voice ID for Talk mode.",
tags: ["media"]
},
"talk.providers.*.voiceAliases": {
label: "Talk Provider Voice Aliases",
help: "Optional provider voice alias map for Talk directives.",
tags: ["media"]
},
"talk.providers.*.modelId": {
label: "Talk Provider Model ID",
help: "Provider default model ID for Talk mode.",
tags: ["models", "media"]
},
"talk.providers.*.outputFormat": {
label: "Talk Provider Output Format",
help: "Provider default output format for Talk mode.",
tags: ["media"]
},
"talk.providers.*.apiKey": {
label: "Talk Provider API Key",
help: "Provider API key for Talk mode.",
tags: [
"security",
"auth",
"media"
],
sensitive: true
},
"talk.apiKey": {
label: "Talk API Key",
help: "Use this legacy ElevenLabs API key for Talk mode only during migration, and keep secrets in env-backed storage. Prefer talk.providers.elevenlabs.apiKey (fallback: ELEVENLABS_API_KEY).",
tags: [
"security",
"auth",
"media"
],
sensitive: true
},
"channels.defaults": {
label: "Channel Defaults",
help: "Default channel behavior applied across providers when provider-specific settings are not set. Use this to enforce consistent baseline policy before per-provider tuning.",
tags: ["network", "channels"]
},
"channels.defaults.groupPolicy": {
label: "Default Group Policy",
help: "Default group policy across channels: \"open\", \"disabled\", or \"allowlist\". Keep \"allowlist\" for safer production setups unless broad group participation is intentional.",
tags: [
"access",
"network",
"channels"
]
},
"channels.defaults.heartbeat": {
label: "Default Heartbeat Visibility",
help: "Default heartbeat visibility settings for status messages emitted by providers/channels. Tune this globally to reduce noisy healthy-state updates while keeping alerts visible.",
tags: [
"network",
"automation",
"channels"
]
},
"channels.defaults.heartbeat.showOk": {
label: "Heartbeat Show OK",
help: "Shows healthy/OK heartbeat status entries when true in channel status outputs. Keep false in noisy environments and enable only when operators need explicit healthy confirmations.",
tags: [
"network",
"automation",
"channels"
]
},
"channels.defaults.heartbeat.showAlerts": {
label: "Heartbeat Show Alerts",
help: "Shows degraded/error heartbeat alerts when true so operator channels surface problems promptly. Keep enabled in production so broken channel states are visible.",
tags: [
"network",
"automation",
"channels"
]
},
"channels.defaults.heartbeat.useIndicator": {
label: "Heartbeat Use Indicator",
help: "Enables concise indicator-style heartbeat rendering instead of verbose status text where supported. Use indicator mode for dense dashboards with many active channels.",
tags: [
"network",
"automation",
"channels"
]
},
"channels.whatsapp": {
label: "WhatsApp",
help: "WhatsApp channel provider configuration for access policy and message batching behavior. Use this section to tune responsiveness and direct-message routing safety for WhatsApp chats.",
tags: ["network", "channels"]
},
"channels.telegram": {
label: "Telegram",
help: "Telegram channel provider configuration including auth tokens, retry behavior, and message rendering controls. Use this section to tune bot behavior for Telegram-specific API semantics.",
tags: ["network", "channels"]
},
"channels.telegram.customCommands": {
label: "Telegram Custom Commands",
help: "Additional Telegram bot menu commands (merged with native; conflicts ignored).",
tags: ["network", "channels"]
},
"channels.discord": {
label: "Discord",
help: "Discord channel provider configuration for bot auth, retry policy, streaming, thread bindings, and optional voice capabilities. Keep privileged intents and advanced features disabled unless needed.",
tags: ["network", "channels"]
},
"channels.slack": {
label: "Slack",
help: "Slack channel provider configuration for bot/app tokens, streaming behavior, and DM policy controls. Keep token handling and thread behavior explicit to avoid noisy workspace interactions.",
tags: ["network", "channels"]
},
"channels.mattermost": {
label: "Mattermost",
help: "Mattermost channel provider configuration for bot credentials, base URL, and message trigger modes. Keep mention/trigger rules strict in high-volume team channels.",
tags: ["network", "channels"]
},
"channels.signal": {
label: "Signal",
help: "Signal channel provider configuration including account identity and DM policy behavior. Keep account mapping explicit so routing remains stable across multi-device setups.",
tags: ["network", "channels"]
},
"channels.imessage": {
label: "iMessage",
help: "iMessage channel provider configuration for CLI integration and DM access policy handling. Use explicit CLI paths when runtime environments have non-standard binary locations.",
tags: ["network", "channels"]
},
"channels.bluebubbles": {
label: "BlueBubbles",
help: "BlueBubbles channel provider configuration used for Apple messaging bridge integrations. Keep DM policy aligned with your trusted sender model in shared deployments.",
tags: ["network", "channels"]
},
"channels.msteams": {
label: "MS Teams",
help: "Microsoft Teams channel provider configuration and provider-specific policy toggles. Use this section to isolate Teams behavior from other enterprise chat providers.",
tags: ["network", "channels"]
},
"channels.modelByChannel": {
label: "Channel Model Overrides",
help: "Map provider -> channel id -> model override (values are provider/model or aliases).",
tags: ["network", "channels"]
},
"channels.irc": {
label: "IRC",
help: "IRC channel provider configuration and compatibility settings for classic IRC transport workflows. Use this section when bridging legacy chat infrastructure into OpenClaw.",
tags: ["network", "channels"]
},
"channels.irc.dmPolicy": {
label: "IRC DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.irc.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.irc.nickserv.enabled": {
label: "IRC NickServ Enabled",
help: "Enable NickServ identify/register after connect (defaults to enabled when password is configured).",
tags: ["network", "channels"]
},
"channels.irc.nickserv.service": {
label: "IRC NickServ Service",
help: "NickServ service nick (default: NickServ).",
tags: ["network", "channels"]
},
"channels.irc.nickserv.password": {
label: "IRC NickServ Password",
help: "NickServ password used for IDENTIFY/REGISTER (sensitive).",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.irc.nickserv.passwordFile": {
label: "IRC NickServ Password File",
help: "Optional file path containing NickServ password.",
tags: [
"security",
"auth",
"network",
"storage",
"channels"
]
},
"channels.irc.nickserv.register": {
label: "IRC NickServ Register",
help: "If true, send NickServ REGISTER on every connect. Use once for initial registration, then disable.",
tags: ["network", "channels"]
},
"channels.irc.nickserv.registerEmail": {
label: "IRC NickServ Register Email",
help: "Email used with NickServ REGISTER (required when register=true).",
tags: ["network", "channels"]
},
"channels.telegram.botToken": {
label: "Telegram Bot Token",
help: "Telegram bot token used to authenticate Bot API requests for this account/provider config. Use secret/env substitution and rotate tokens if exposure is suspected.",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.telegram.dmPolicy": {
label: "Telegram DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.telegram.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.telegram.configWrites": {
label: "Telegram Config Writes",
help: "Allow Telegram to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.telegram.commands.native": {
label: "Telegram Native Commands",
help: "Override native commands for Telegram (bool or \"auto\").",
tags: ["network", "channels"]
},
"channels.telegram.commands.nativeSkills": {
label: "Telegram Native Skill Commands",
help: "Override native skill commands for Telegram (bool or \"auto\").",
tags: ["network", "channels"]
},
"channels.telegram.streaming": {
label: "Telegram Streaming Mode",
help: "Unified Telegram stream preview mode: \"off\" | \"partial\" | \"block\" | \"progress\" (default: \"partial\"). \"progress\" maps to \"partial\" on Telegram. Legacy boolean/streamMode keys are auto-mapped.",
tags: ["network", "channels"]
},
"channels.telegram.retry.attempts": {
label: "Telegram Retry Attempts",
help: "Max retry attempts for outbound Telegram API calls (default: 3).",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.telegram.retry.minDelayMs": {
label: "Telegram Retry Min Delay (ms)",
help: "Minimum retry delay in ms for Telegram outbound calls.",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.telegram.retry.maxDelayMs": {
label: "Telegram Retry Max Delay (ms)",
help: "Maximum retry delay cap in ms for Telegram outbound calls.",
tags: [
"network",
"reliability",
"performance",
"channels"
]
},
"channels.telegram.retry.jitter": {
label: "Telegram Retry Jitter",
help: "Jitter factor (0-1) applied to Telegram retry delays.",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.telegram.network.autoSelectFamily": {
label: "Telegram autoSelectFamily",
help: "Override Node autoSelectFamily for Telegram (true=enable, false=disable).",
tags: ["network", "channels"]
},
"channels.telegram.timeoutSeconds": {
label: "Telegram API Timeout (seconds)",
help: "Max seconds before Telegram API requests are aborted (default: 500 per grammY).",
tags: [
"network",
"performance",
"channels"
]
},
"channels.telegram.silentErrorReplies": {
label: "Telegram Silent Error Replies",
help: "When true, Telegram bot replies marked as errors are sent silently (no notification sound). Default: false.",
tags: ["network", "channels"]
},
"channels.telegram.apiRoot": {
label: "Telegram API Root URL",
help: "Custom Telegram Bot API root URL. Use for self-hosted Bot API servers (https://github.com/tdlib/telegram-bot-api) or reverse proxies in regions where api.telegram.org is blocked.",
tags: ["network", "channels"]
},
"channels.telegram.autoTopicLabel": {
label: "Telegram Auto Topic Label",
help: "Auto-rename DM forum topics on first message using LLM. Default: true. Set to false to disable, or use object form { enabled: true, prompt: '...' } for custom prompt.",
tags: ["network", "channels"]
},
"channels.telegram.autoTopicLabel.enabled": {
label: "Telegram Auto Topic Label Enabled",
help: "Whether auto topic labeling is enabled. Default: true.",
tags: ["network", "channels"]
},
"channels.telegram.autoTopicLabel.prompt": {
label: "Telegram Auto Topic Label Prompt",
help: "Custom prompt for LLM-based topic naming. The user message is appended after the prompt.",
tags: ["network", "channels"]
},
"channels.telegram.capabilities.inlineButtons": {
label: "Telegram Inline Buttons",
help: "Enable Telegram inline button components for supported command and interaction surfaces. Disable if your deployment needs plain-text-only compatibility behavior.",
tags: ["network", "channels"]
},
"channels.telegram.execApprovals": {
label: "Telegram Exec Approvals",
help: "Telegram-native exec approval routing and approver authorization. Enable this only when Telegram should act as an explicit exec-approval client for the selected bot account.",
tags: ["network", "channels"]
},
"channels.telegram.execApprovals.enabled": {
label: "Telegram Exec Approvals Enabled",
help: "Enable Telegram exec approvals for this account. When false or unset, Telegram messages/buttons cannot approve exec requests.",
tags: ["network", "channels"]
},
"channels.telegram.execApprovals.approvers": {
label: "Telegram Exec Approval Approvers",
help: "Telegram user IDs allowed to approve exec requests for this bot account. Use numeric Telegram user IDs; prompts are only delivered to these approvers when target includes dm.",
tags: ["network", "channels"]
},
"channels.telegram.execApprovals.agentFilter": {
label: "Telegram Exec Approval Agent Filter",
help: "Optional allowlist of agent IDs eligible for Telegram exec approvals, for example `[\"main\", \"ops-agent\"]`. Use this to keep approval prompts scoped to the agents you actually operate from Telegram.",
tags: ["network", "channels"]
},
"channels.telegram.execApprovals.sessionFilter": {
label: "Telegram Exec Approval Session Filter",
help: "Optional session-key filters matched as substring or regex-style patterns before Telegram approval routing is used. Use narrow patterns so Telegram approvals only appear for intended sessions.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.telegram.execApprovals.target": {
label: "Telegram Exec Approval Target",
help: "Controls where Telegram approval prompts are sent: \"dm\" sends to approver DMs (default), \"channel\" sends to the originating Telegram chat/topic, and \"both\" sends to both. Channel delivery exposes the command text to the chat, so only use it in trusted groups/topics.",
tags: ["network", "channels"]
},
"channels.telegram.threadBindings.enabled": {
label: "Telegram Thread Binding Enabled",
help: "Enable Telegram conversation binding features (/focus, /unfocus, /agents, and /session idle|max-age). Overrides session.threadBindings.enabled when set.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.telegram.threadBindings.idleHours": {
label: "Telegram Thread Binding Idle Timeout (hours)",
help: "Inactivity window in hours for Telegram bound sessions. Set 0 to disable idle auto-unfocus (default: 24). Overrides session.threadBindings.idleHours when set.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.telegram.threadBindings.maxAgeHours": {
label: "Telegram Thread Binding Max Age (hours)",
help: "Optional hard max age in hours for Telegram bound sessions. Set 0 to disable hard cap (default: 0). Overrides session.threadBindings.maxAgeHours when set.",
tags: [
"network",
"performance",
"storage",
"channels"
]
},
"channels.telegram.threadBindings.spawnSubagentSessions": {
label: "Telegram Thread-Bound Subagent Spawn",
help: "Allow subagent spawns with thread=true to auto-bind Telegram current conversations when supported.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.telegram.threadBindings.spawnAcpSessions": {
label: "Telegram Thread-Bound ACP Spawn",
help: "Allow ACP spawns with thread=true to auto-bind Telegram current conversations when supported.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.whatsapp.dmPolicy": {
label: "WhatsApp DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.whatsapp.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.whatsapp.selfChatMode": {
label: "WhatsApp Self-Phone Mode",
help: "Same-phone setup (bot uses your personal WhatsApp number).",
tags: ["network", "channels"]
},
"channels.whatsapp.debounceMs": {
label: "WhatsApp Message Debounce (ms)",
help: "Debounce window (ms) for batching rapid consecutive messages from the same sender (0 to disable).",
tags: [
"network",
"performance",
"channels"
]
},
"channels.whatsapp.configWrites": {
label: "WhatsApp Config Writes",
help: "Allow WhatsApp to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.signal.dmPolicy": {
label: "Signal DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.signal.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.signal.configWrites": {
label: "Signal Config Writes",
help: "Allow Signal to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.imessage.dmPolicy": {
label: "iMessage DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.imessage.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.imessage.configWrites": {
label: "iMessage Config Writes",
help: "Allow iMessage to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.bluebubbles.dmPolicy": {
label: "BlueBubbles DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.bluebubbles.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.msteams.configWrites": {
label: "MS Teams Config Writes",
help: "Allow Microsoft Teams to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.irc.configWrites": {
label: "IRC Config Writes",
help: "Allow IRC to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.discord.dmPolicy": {
label: "Discord DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.discord.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.discord.dm.policy": {
label: "Discord DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.discord.allowFrom=[\"*\"] (legacy: channels.discord.dm.allowFrom).",
tags: [
"access",
"network",
"channels"
]
},
"channels.discord.configWrites": {
label: "Discord Config Writes",
help: "Allow Discord to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.discord.proxy": {
label: "Discord Proxy URL",
help: "Proxy URL for Discord gateway + API requests (app-id lookup and allowlist resolution). Set per account via channels.discord.accounts.<id>.proxy.",
tags: ["network", "channels"]
},
"channels.discord.commands.native": {
label: "Discord Native Commands",
help: "Override native commands for Discord (bool or \"auto\").",
tags: ["network", "channels"]
},
"channels.discord.commands.nativeSkills": {
label: "Discord Native Skill Commands",
help: "Override native skill commands for Discord (bool or \"auto\").",
tags: ["network", "channels"]
},
"channels.discord.streaming": {
label: "Discord Streaming Mode",
help: "Unified Discord stream preview mode: \"off\" | \"partial\" | \"block\" | \"progress\". \"progress\" maps to \"partial\" on Discord. Legacy boolean/streamMode keys are auto-mapped.",
tags: ["network", "channels"]
},
"channels.discord.streamMode": {
label: "Discord Stream Mode (Legacy)",
help: "Legacy Discord preview mode alias (off | partial | block); auto-migrated to channels.discord.streaming.",
tags: ["network", "channels"]
},
"channels.discord.draftChunk.minChars": {
label: "Discord Draft Chunk Min Chars",
help: "Minimum chars before emitting a Discord stream preview update when channels.discord.streaming=\"block\" (default: 200).",
tags: ["network", "channels"]
},
"channels.discord.draftChunk.maxChars": {
label: "Discord Draft Chunk Max Chars",
help: "Target max size for a Discord stream preview chunk when channels.discord.streaming=\"block\" (default: 800; clamped to channels.discord.textChunkLimit).",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.draftChunk.breakPreference": {
label: "Discord Draft Chunk Break Preference",
help: "Preferred breakpoints for Discord draft chunks (paragraph | newline | sentence). Default: paragraph.",
tags: ["network", "channels"]
},
"channels.discord.retry.attempts": {
label: "Discord Retry Attempts",
help: "Max retry attempts for outbound Discord API calls (default: 3).",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.discord.retry.minDelayMs": {
label: "Discord Retry Min Delay (ms)",
help: "Minimum retry delay in ms for Discord outbound calls.",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.discord.retry.maxDelayMs": {
label: "Discord Retry Max Delay (ms)",
help: "Maximum retry delay cap in ms for Discord outbound calls.",
tags: [
"network",
"reliability",
"performance",
"channels"
]
},
"channels.discord.retry.jitter": {
label: "Discord Retry Jitter",
help: "Jitter factor (0-1) applied to Discord retry delays.",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.discord.maxLinesPerMessage": {
label: "Discord Max Lines Per Message",
help: "Soft max line count per Discord message (default: 17).",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.inboundWorker.runTimeoutMs": {
label: "Discord Inbound Worker Timeout (ms)",
help: "Optional queued Discord inbound worker timeout in ms. This is separate from Carbon listener timeouts; defaults to 1800000 and can be disabled with 0. Set per account via channels.discord.accounts.<id>.inboundWorker.runTimeoutMs.",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.eventQueue.listenerTimeout": {
label: "Discord EventQueue Listener Timeout (ms)",
help: "Canonical Discord listener timeout control in ms for gateway normalization/enqueue handlers. Default is 120000 in OpenClaw; set per account via channels.discord.accounts.<id>.eventQueue.listenerTimeout.",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.eventQueue.maxQueueSize": {
label: "Discord EventQueue Max Queue Size",
help: "Optional Discord EventQueue capacity override (max queued events before backpressure). Set per account via channels.discord.accounts.<id>.eventQueue.maxQueueSize.",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.eventQueue.maxConcurrency": {
label: "Discord EventQueue Max Concurrency",
help: "Optional Discord EventQueue concurrency override (max concurrent handler executions). Set per account via channels.discord.accounts.<id>.eventQueue.maxConcurrency.",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.threadBindings.enabled": {
label: "Discord Thread Binding Enabled",
help: "Enable Discord thread binding features (/focus, bound-thread routing/delivery, and thread-bound subagent sessions). Overrides session.threadBindings.enabled when set.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.discord.threadBindings.idleHours": {
label: "Discord Thread Binding Idle Timeout (hours)",
help: "Inactivity window in hours for Discord thread-bound sessions (/focus and spawned thread sessions). Set 0 to disable idle auto-unfocus (default: 24). Overrides session.threadBindings.idleHours when set.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.discord.threadBindings.maxAgeHours": {
label: "Discord Thread Binding Max Age (hours)",
help: "Optional hard max age in hours for Discord thread-bound sessions. Set 0 to disable hard cap (default: 0). Overrides session.threadBindings.maxAgeHours when set.",
tags: [
"network",
"performance",
"storage",
"channels"
]
},
"channels.discord.threadBindings.spawnSubagentSessions": {
label: "Discord Thread-Bound Subagent Spawn",
help: "Allow subagent spawns with thread=true to auto-create and bind Discord threads (default: false; opt-in). Set true to enable thread-bound subagent spawns for this account/channel.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.discord.threadBindings.spawnAcpSessions": {
label: "Discord Thread-Bound ACP Spawn",
help: "Allow /acp spawn to auto-create and bind Discord threads for ACP sessions (default: false; opt-in). Set true to enable thread-bound ACP spawns for this account/channel.",
tags: [
"network",
"storage",
"channels"
]
},
"channels.discord.ui.components.accentColor": {
label: "Discord Component Accent Color",
help: "Accent color for Discord component containers (hex). Set per account via channels.discord.accounts.<id>.ui.components.accentColor.",
tags: ["network", "channels"]
},
"channels.discord.intents.presence": {
label: "Discord Presence Intent",
help: "Enable the Guild Presences privileged intent. Must also be enabled in the Discord Developer Portal. Allows tracking user activities (e.g. Spotify). Default: false.",
tags: ["network", "channels"]
},
"channels.discord.intents.guildMembers": {
label: "Discord Guild Members Intent",
help: "Enable the Guild Members privileged intent. Must also be enabled in the Discord Developer Portal. Default: false.",
tags: ["network", "channels"]
},
"channels.discord.voice.enabled": {
label: "Discord Voice Enabled",
help: "Enable Discord voice channel conversations (default: true). Omit channels.discord.voice to keep voice support disabled for the account.",
tags: ["network", "channels"]
},
"channels.discord.voice.autoJoin": {
label: "Discord Voice Auto-Join",
help: "Voice channels to auto-join on startup (list of guildId/channelId entries).",
tags: ["network", "channels"]
},
"channels.discord.voice.daveEncryption": {
label: "Discord Voice DAVE Encryption",
help: "Toggle DAVE end-to-end encryption for Discord voice joins (default: true in @discordjs/voice; Discord may require this).",
tags: ["network", "channels"]
},
"channels.discord.voice.decryptionFailureTolerance": {
label: "Discord Voice Decrypt Failure Tolerance",
help: "Consecutive decrypt failures before DAVE attempts session recovery (passed to @discordjs/voice; default: 24).",
tags: ["network", "channels"]
},
"channels.discord.voice.tts": {
label: "Discord Voice Text-to-Speech",
help: "Optional TTS overrides for Discord voice playback (merged with messages.tts).",
tags: [
"network",
"media",
"channels"
]
},
"channels.discord.pluralkit.enabled": {
label: "Discord PluralKit Enabled",
help: "Resolve PluralKit proxied messages and treat system members as distinct senders.",
tags: ["network", "channels"]
},
"channels.discord.pluralkit.token": {
label: "Discord PluralKit Token",
help: "Optional PluralKit token for resolving private systems or members.",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.discord.activity": {
label: "Discord Presence Activity",
help: "Discord presence activity text (defaults to custom status).",
tags: ["network", "channels"]
},
"channels.discord.status": {
label: "Discord Presence Status",
help: "Discord presence status (online, dnd, idle, invisible).",
tags: ["network", "channels"]
},
"channels.discord.autoPresence.enabled": {
label: "Discord Auto Presence Enabled",
help: "Enable automatic Discord bot presence updates based on runtime/model availability signals. When enabled: healthy=>online, degraded/unknown=>idle, exhausted/unavailable=>dnd.",
tags: ["network", "channels"]
},
"channels.discord.autoPresence.intervalMs": {
label: "Discord Auto Presence Check Interval (ms)",
help: "How often to evaluate Discord auto-presence state in milliseconds (default: 30000).",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.autoPresence.minUpdateIntervalMs": {
label: "Discord Auto Presence Min Update Interval (ms)",
help: "Minimum time between actual Discord presence update calls in milliseconds (default: 15000). Prevents status spam on noisy state changes.",
tags: [
"network",
"performance",
"channels"
]
},
"channels.discord.autoPresence.healthyText": {
label: "Discord Auto Presence Healthy Text",
help: "Optional custom status text while runtime is healthy (online). If omitted, falls back to static channels.discord.activity when set.",
tags: [
"network",
"reliability",
"channels"
]
},
"channels.discord.autoPresence.degradedText": {
label: "Discord Auto Presence Degraded Text",
help: "Optional custom status text while runtime/model availability is degraded or unknown (idle).",
tags: ["network", "channels"]
},
"channels.discord.autoPresence.exhaustedText": {
label: "Discord Auto Presence Exhausted Text",
help: "Optional custom status text while runtime detects exhausted/unavailable model quota (dnd). Supports {reason} template placeholder.",
tags: ["network", "channels"]
},
"channels.discord.activityType": {
label: "Discord Presence Activity Type",
help: "Discord presence activity type (0=Playing,1=Streaming,2=Listening,3=Watching,4=Custom,5=Competing).",
tags: ["network", "channels"]
},
"channels.discord.activityUrl": {
label: "Discord Presence Activity URL",
help: "Discord presence streaming URL (required for activityType=1).",
tags: ["network", "channels"]
},
"channels.slack.dm.policy": {
label: "Slack DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.slack.allowFrom=[\"*\"] (legacy: channels.slack.dm.allowFrom).",
tags: [
"access",
"network",
"channels"
]
},
"channels.slack.dmPolicy": {
label: "Slack DM Policy",
help: "Direct message access control (\"pairing\" recommended). \"open\" requires channels.slack.allowFrom=[\"*\"].",
tags: [
"access",
"network",
"channels"
]
},
"channels.slack.configWrites": {
label: "Slack Config Writes",
help: "Allow Slack to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.slack.commands.native": {
label: "Slack Native Commands",
help: "Override native commands for Slack (bool or \"auto\").",
tags: ["network", "channels"]
},
"channels.slack.commands.nativeSkills": {
label: "Slack Native Skill Commands",
help: "Override native skill commands for Slack (bool or \"auto\").",
tags: ["network", "channels"]
},
"channels.slack.allowBots": {
label: "Slack Allow Bot Messages",
help: "Allow bot-authored messages to trigger Slack replies (default: false).",
tags: [
"access",
"network",
"channels"
]
},
"channels.discord.allowBots": {
label: "Discord Allow Bot Messages",
help: "Allow bot-authored messages to trigger Discord replies (default: false). Set \"mentions\" to only accept bot messages that mention the bot.",
tags: [
"access",
"network",
"channels"
]
},
"channels.matrix.allowBots": {
label: "Matrix Allow Bot Messages",
help: "Allow messages from other configured Matrix bot accounts to trigger replies (default: false). Set \"mentions\" to only accept bot messages that visibly mention this bot.",
tags: [
"access",
"network",
"channels"
]
},
"channels.discord.token": {
label: "Discord Bot Token",
help: "Discord bot token used for gateway and REST API authentication for this provider account. Keep this secret out of committed config and rotate immediately after any leak.",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.slack.botToken": {
label: "Slack Bot Token",
help: "Slack bot token used for standard chat actions in the configured workspace. Keep this credential scoped and rotate if workspace app permissions change.",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.slack.appToken": {
label: "Slack App Token",
help: "Slack app-level token used for Socket Mode connections and event transport when enabled. Use least-privilege app scopes and store this token as a secret.",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.slack.userToken": {
label: "Slack User Token",
help: "Optional Slack user token for workflows requiring user-context API access beyond bot permissions. Use sparingly and audit scopes because this token can carry broader authority.",
tags: [
"security",
"auth",
"network",
"channels"
],
sensitive: true
},
"channels.slack.userTokenReadOnly": {
label: "Slack User Token Read Only",
help: "When true, treat configured Slack user token usage as read-only helper behavior where possible. Keep enabled if you only need supplemental reads without user-context writes.",
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.slack.capabilities.interactiveReplies": {
label: "Slack Interactive Replies",
help: "Enable agent-authored Slack interactive reply directives (`[[slack_buttons: ...]]`, `[[slack_select: ...]]`). Default: false.",
tags: ["network", "channels"]
},
"channels.slack.streaming": {
label: "Slack Streaming Mode",
help: "Unified Slack stream preview mode: \"off\" | \"partial\" | \"block\" | \"progress\". Legacy boolean/streamMode keys are auto-mapped.",
tags: ["network", "channels"]
},
"channels.slack.nativeStreaming": {
label: "Slack Native Streaming",
help: "Enable native Slack text streaming (chat.startStream/chat.appendStream/chat.stopStream) when channels.slack.streaming is partial (default: true).",
tags: ["network", "channels"]
},
"channels.slack.streamMode": {
label: "Slack Stream Mode (Legacy)",
help: "Legacy Slack preview mode alias (replace | status_final | append); auto-migrated to channels.slack.streaming.",
tags: ["network", "channels"]
},
"channels.slack.thread.historyScope": {
label: "Slack Thread History Scope",
help: "Scope for Slack thread history context (\"thread\" isolates per thread; \"channel\" reuses channel history).",
tags: ["network", "channels"]
},
"channels.slack.thread.inheritParent": {
label: "Slack Thread Parent Inheritance",
help: "If true, Slack thread sessions inherit the parent channel transcript (default: false).",
tags: ["network", "channels"]
},
"channels.slack.thread.initialHistoryLimit": {
label: "Slack Thread Initial History Limit",
help: "Maximum number of existing Slack thread messages to fetch when starting a new thread session (default: 20, set to 0 to disable).",
tags: [
"network",
"performance",
"channels"
]
},
"channels.mattermost.botToken": {
label: "Mattermost Bot Token",
help: "Bot token from Mattermost System Console -> Integrations -> Bot Accounts.",
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.mattermost.baseUrl": {
label: "Mattermost Base URL",
help: "Base URL for your Mattermost server (e.g., https://chat.example.com).",
placeholder: "https://chat.example.com",
tags: ["network", "channels"]
},
"channels.mattermost.configWrites": {
label: "Mattermost Config Writes",
help: "Allow Mattermost to write config in response to channel events/commands (default: true).",
tags: ["network", "channels"]
},
"channels.mattermost.chatmode": {
label: "Mattermost Chat Mode",
help: "Reply to channel messages on mention (\"oncall\"), on trigger chars (\">\" or \"!\") (\"onchar\"), or on every message (\"onmessage\").",
tags: ["network", "channels"]
},
"channels.mattermost.oncharPrefixes": {
label: "Mattermost Onchar Prefixes",
help: "Trigger prefixes for onchar mode (default: [\">\", \"!\"]).",
tags: ["network", "channels"]
},
"channels.mattermost.requireMention": {
label: "Mattermost Require Mention",
help: "Require @mention in channels before responding (default: true).",
tags: ["network", "channels"]
},
"channels.signal.account": {
label: "Signal Account",
help: "Signal account identifier (phone/number handle) used to bind this channel config to a specific Signal identity. Keep this aligned with your linked device/session state.",
tags: ["network", "channels"]
},
"channels.imessage.cliPath": {
label: "iMessage CLI Path",
help: "Filesystem path to the iMessage bridge CLI binary used for send/receive operations. Set explicitly when the binary is not on PATH in service runtime environments.",
tags: [
"network",
"storage",
"channels"
]
},
"agents.list[].skills": {
label: "Agent Skill Filter",
help: "Optional allowlist of skills for this agent (omit = all skills; empty = no skills).",
tags: ["advanced"]
},
"agents.list[].identity.avatar": {
label: "Agent Avatar",
help: "Avatar image path (relative to the agent workspace only) or a remote URL/data URL.",
placeholder: "avatars/openclaw.png",
tags: ["advanced"]
},
"agents.list[].heartbeat.suppressToolErrorWarnings": {
label: "Agent Heartbeat Suppress Tool Error Warnings",
help: "Suppress tool error warning payloads during heartbeat runs.",
tags: ["automation"]
},
"agents.list[].sandbox.browser.network": {
label: "Agent Sandbox Browser Network",
help: "Per-agent override for sandbox browser Docker network.",
tags: ["storage"]
},
"agents.list[].sandbox.browser.cdpSourceRange": {
label: "Agent Sandbox Browser CDP Source Port Range",
help: "Per-agent override for CDP source CIDR allowlist.",
tags: ["storage"]
},
"agents.list[].sandbox.docker.dangerouslyAllowContainerNamespaceJoin": {
label: "Agent Sandbox Docker Allow Container Namespace Join",
help: "Per-agent DANGEROUS override for container namespace joins in sandbox Docker network mode.",
tags: [
"security",
"access",
"storage",
"advanced"
]
},
"discovery.mdns.mode": {
label: "mDNS Discovery Mode",
help: "mDNS broadcast mode (\"minimal\" default, \"full\" includes cliPath/sshPort, \"off\" disables mDNS).",
tags: ["network"]
},
"plugins.enabled": {
label: "Enable Plugins",
help: "Enable or disable plugin/extension loading globally during startup and config reload (default: true). Keep enabled only when extension capabilities are required by your deployment.",
tags: ["advanced"]
},
"plugins.allow": {
label: "Plugin Allowlist",
help: "Optional allowlist of plugin IDs; when set, only listed plugins are eligible to load. Use this to enforce approved extension inventories in controlled environments.",
tags: ["access"]
},
"plugins.deny": {
label: "Plugin Denylist",
help: "Optional denylist of plugin IDs that are blocked even if allowlists or paths include them. Use deny rules for emergency rollback and hard blocks on risky plugins.",
tags: ["access"]
},
"plugins.load": {
label: "Plugin Loader",
help: "Plugin loader configuration group for specifying filesystem paths where plugins are discovered. Keep load paths explicit and reviewed to avoid accidental untrusted extension loading.",
tags: ["advanced"]
},
"plugins.load.paths": {
label: "Plugin Load Paths",
help: "Additional plugin files or directories scanned by the loader beyond built-in defaults. Use dedicated extension directories and avoid broad paths with unrelated executable content.",
tags: ["storage"]
},
"plugins.slots": {
label: "Plugin Slots",
help: "Selects which plugins own exclusive runtime slots such as memory so only one plugin provides that capability. Use explicit slot ownership to avoid overlapping providers with conflicting behavior.",
tags: ["advanced"]
},
"plugins.slots.memory": {
label: "Memory Plugin",
help: "Select the active memory plugin by id, or \"none\" to disable memory plugins.",
tags: ["advanced"]
},
"plugins.slots.contextEngine": {
label: "Context Engine Plugin",
help: "Selects the active context engine plugin by id so one plugin provides context orchestration behavior.",
tags: ["advanced"]
},
"plugins.entries": {
label: "Plugin Entries",
help: "Per-plugin settings keyed by plugin ID including enablement and plugin-specific runtime configuration payloads. Use this for scoped plugin tuning without changing global loader policy.",
tags: ["advanced"]
},
"plugins.entries.*.enabled": {
label: "Plugin Enabled",
help: "Per-plugin enablement override for a specific entry, applied on top of global plugin policy (restart required). Use this to stage plugin rollout gradually across environments.",
tags: ["advanced"]
},
"plugins.entries.*.hooks": {
label: "Plugin Hook Policy",
help: "Per-plugin typed hook policy controls for core-enforced safety gates. Use this to constrain high-impact hook categories without disabling the entire plugin.",
tags: ["advanced"]
},
"plugins.entries.*.hooks.allowPromptInjection": {
label: "Allow Prompt Injection Hooks",
help: "Controls whether this plugin may mutate prompts through typed hooks. Set false to block `before_prompt_build` and ignore prompt-mutating fields from legacy `before_agent_start`, while preserving legacy `modelOverride` and `providerOverride` behavior.",
tags: ["access"]
},
"plugins.entries.*.subagent": {
label: "Plugin Subagent Policy",
help: "Per-plugin subagent runtime controls for model override trust and allowlists. Keep this unset unless a plugin must explicitly steer subagent model selection.",
tags: ["advanced"]
},
"plugins.entries.*.subagent.allowModelOverride": {
label: "Allow Plugin Subagent Model Override",
help: "Explicitly allows this plugin to request provider/model overrides in background subagent runs. Keep false unless the plugin is trusted to steer model selection.",
tags: ["access"]
},
"plugins.entries.*.subagent.allowedModels": {
label: "Plugin Subagent Allowed Models",
help: "Allowed override targets for trusted plugin subagent runs as canonical \"provider/model\" refs. Use \"*\" only when you intentionally allow any model.",
tags: ["access"]
},
"plugins.entries.*.apiKey": {
label: "Plugin API Key",
help: "Optional API key field consumed by plugins that accept direct key configuration in entry settings. Use secret/env substitution and avoid committing real credentials into config files.",
tags: ["security", "auth"]
},
"plugins.entries.*.env": {
label: "Plugin Environment Variables",
help: "Per-plugin environment variable map injected for that plugin runtime context only. Use this to scope provider credentials to one plugin instead of sharing global process environment.",
tags: ["advanced"]
},
"plugins.entries.*.config": {
label: "Plugin Config",
help: "Plugin-defined configuration payload interpreted by that plugin's own schema and validation rules. Use only documented fields from the plugin to prevent ignored or invalid settings.",
tags: ["advanced"]
},
"plugins.installs": {
label: "Plugin Install Records",
help: "CLI-managed install metadata (used by `openclaw plugins update` to locate install sources).",
tags: ["advanced"]
},
"plugins.installs.*.source": {
label: "Plugin Install Source",
help: "Install source (\"npm\", \"archive\", or \"path\").",
tags: ["advanced"]
},
"plugins.installs.*.spec": {
label: "Plugin Install Spec",
help: "Original npm spec used for install (if source is npm).",
tags: ["advanced"]
},
"plugins.installs.*.sourcePath": {
label: "Plugin Install Source Path",
help: "Original archive/path used for install (if any).",
tags: ["storage"]
},
"plugins.installs.*.installPath": {
label: "Plugin Install Path",
help: "Resolved install directory (usually ~/.openclaw/extensions/<id>).",
tags: ["storage"]
},
"plugins.installs.*.version": {
label: "Plugin Install Version",
help: "Version recorded at install time (if available).",
tags: ["advanced"]
},
"plugins.installs.*.resolvedName": {
label: "Plugin Resolved Package Name",
help: "Resolved npm package name from the fetched artifact.",
tags: ["advanced"]
},
"plugins.installs.*.resolvedVersion": {
label: "Plugin Resolved Package Version",
help: "Resolved npm package version from the fetched artifact (useful for non-pinned specs).",
tags: ["advanced"]
},
"plugins.installs.*.resolvedSpec": {
label: "Plugin Resolved Package Spec",
help: "Resolved exact npm spec (<name>@<version>) from the fetched artifact.",
tags: ["advanced"]
},
"plugins.installs.*.integrity": {
label: "Plugin Resolved Integrity",
help: "Resolved npm dist integrity hash for the fetched artifact (if reported by npm).",
tags: ["advanced"]
},
"plugins.installs.*.shasum": {
label: "Plugin Resolved Shasum",
help: "Resolved npm dist shasum for the fetched artifact (if reported by npm).",
tags: ["advanced"]
},
"plugins.installs.*.resolvedAt": {
label: "Plugin Resolution Time",
help: "ISO timestamp when npm package metadata was last resolved for this install record.",
tags: ["advanced"]
},
"plugins.installs.*.installedAt": {
label: "Plugin Install Time",
help: "ISO timestamp of last install/update.",
tags: ["advanced"]
},
"plugins.installs.*.marketplaceName": {
label: "Plugin Marketplace Name",
help: "Marketplace display name recorded for marketplace-backed plugin installs (if available).",
tags: ["advanced"]
},
"plugins.installs.*.marketplaceSource": {
label: "Plugin Marketplace Source",
help: "Original marketplace source used to resolve the install (for example a repo path or Git URL).",
tags: ["advanced"]
},
"plugins.installs.*.marketplacePlugin": {
label: "Plugin Marketplace Plugin",
help: "Plugin entry name inside the source marketplace, used for later updates.",
tags: ["advanced"]
},
"models.providers.*.headers.*": {
sensitive: true,
tags: ["security", "models"]
},
"agents.defaults.sandbox.ssh.identityData": {
sensitive: true,
tags: ["security", "storage"]
},
"agents.defaults.sandbox.ssh.certificateData": {
sensitive: true,
tags: ["security", "storage"]
},
"agents.defaults.sandbox.ssh.knownHostsData": {
sensitive: true,
tags: ["security", "storage"]
},
"agents.list[].memorySearch.remote.apiKey": {
sensitive: true,
tags: ["security", "auth"]
},
"agents.list[].sandbox.ssh.identityData": {
sensitive: true,
tags: ["security", "storage"]
},
"agents.list[].sandbox.ssh.certificateData": {
sensitive: true,
tags: ["security", "storage"]
},
"agents.list[].sandbox.ssh.knownHostsData": {
sensitive: true,
tags: ["security", "storage"]
},
"tools.web.search.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"tools.web.search.brave.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"tools.web.search.firecrawl.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"tools.web.search.gemini.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"tools.web.search.grok.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"tools.web.search.kimi.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"tools.web.search.perplexity.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"tools"
]
},
"messages.tts.elevenlabs.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"media"
]
},
"messages.tts.openai.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"media"
]
},
"channels.telegram.webhookSecret": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.telegram.accounts.*.botToken": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.telegram.accounts.*.webhookSecret": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.discord.voice.tts.elevenlabs.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"media",
"channels"
]
},
"channels.discord.voice.tts.openai.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"media",
"channels"
]
},
"channels.discord.accounts.*.token": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.discord.accounts.*.voice.tts.elevenlabs.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"media",
"channels"
]
},
"channels.discord.accounts.*.voice.tts.openai.apiKey": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"media",
"channels"
]
},
"channels.discord.accounts.*.pluralkit.token": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.irc.password": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.irc.accounts.*.password": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.irc.accounts.*.nickserv.password": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.googlechat.serviceAccount": {
sensitive: true,
tags: [
"security",
"network",
"channels"
]
},
"channels.googlechat.serviceAccountRef": {
sensitive: true,
tags: [
"security",
"network",
"channels"
]
},
"channels.googlechat.accounts.*.serviceAccount": {
sensitive: true,
tags: [
"security",
"network",
"channels"
]
},
"channels.googlechat.accounts.*.serviceAccountRef": {
sensitive: true,
tags: [
"security",
"network",
"channels"
]
},
"channels.slack.signingSecret": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.slack.accounts.*.signingSecret": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.slack.accounts.*.botToken": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.slack.accounts.*.appToken": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.slack.accounts.*.userToken": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.bluebubbles.password": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.bluebubbles.accounts.*.password": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"channels.msteams.appPassword": {
sensitive: true,
tags: [
"security",
"auth",
"network",
"channels"
]
},
"skills.entries.*.apiKey": {
sensitive: true,
tags: ["security", "auth"]
}
},
version: "2026.3.23",
generatedAt: "2026-03-22T21:17:33.302Z"
};
//#endregion
//#region src/config/schema.shared.ts
function schemaHasChildren(schema) {
if (schema.properties && Object.keys(schema.properties).length > 0) return true;
if (schema.additionalProperties && typeof schema.additionalProperties === "object") return true;
if (Array.isArray(schema.items)) return schema.items.some((entry) => typeof entry === "object" && entry !== null);
for (const branch of [
schema.oneOf,
schema.anyOf,
schema.allOf
]) if (branch?.some((entry) => entry && typeof entry === "object" && schemaHasChildren(entry))) return true;
return Boolean(schema.items && typeof schema.items === "object");
}
function findWildcardHintMatch(params) {
const targetParts = params.splitPath(params.path);
let bestMatch;
for (const [hintPath, hint] of Object.entries(params.uiHints)) {
const hintParts = params.splitPath(hintPath);
if (hintParts.length !== targetParts.length) continue;
let wildcardCount = 0;
let matches = true;
for (let index = 0; index < hintParts.length; index += 1) {
const hintPart = hintParts[index];
if (hintPart === targetParts[index]) continue;
if (hintPart === "*") {
wildcardCount += 1;
continue;
}
matches = false;
break;
}
if (!matches) continue;
if (!bestMatch || wildcardCount < bestMatch.wildcardCount) bestMatch = {
path: hintPath,
hint,
wildcardCount
};
}
return bestMatch ? {
path: bestMatch.path,
hint: bestMatch.hint
} : null;
}
//#endregion
//#region src/config/schema.ts
const FORBIDDEN_LOOKUP_SEGMENTS = new Set([
"__proto__",
"prototype",
"constructor"
]);
const LOOKUP_SCHEMA_STRING_KEYS = new Set([
"$id",
"$schema",
"title",
"description",
"format",
"pattern",
"contentEncoding",
"contentMediaType"
]);
const LOOKUP_SCHEMA_NUMBER_KEYS = new Set([
"minimum",
"maximum",
"exclusiveMinimum",
"exclusiveMaximum",
"multipleOf",
"minLength",
"maxLength",
"minItems",
"maxItems",
"minProperties",
"maxProperties"
]);
const LOOKUP_SCHEMA_BOOLEAN_KEYS = new Set([
"additionalProperties",
"uniqueItems",
"deprecated",
"readOnly",
"writeOnly"
]);
const MAX_LOOKUP_PATH_SEGMENTS = 32;
function cloneSchema(value) {
if (typeof structuredClone === "function") return structuredClone(value);
return JSON.parse(JSON.stringify(value));
}
function asSchemaObject(value) {
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
return value;
}
function isObjectSchema(schema) {
const type = schema.type;
if (type === "object") return true;
if (Array.isArray(type) && type.includes("object")) return true;
return Boolean(schema.properties || schema.additionalProperties);
}
function mergeObjectSchema(base, extension) {
const mergedRequired = new Set([...base.required ?? [], ...extension.required ?? []]);
const merged = {
...base,
...extension,
properties: {
...base.properties,
...extension.properties
}
};
if (mergedRequired.size > 0) merged.required = Array.from(mergedRequired);
const additional = extension.additionalProperties ?? base.additionalProperties;
if (additional !== void 0) merged.additionalProperties = additional;
return merged;
}
function collectExtensionHintKeys(hints, plugins, channels) {
const pluginPrefixes = plugins.map((plugin) => plugin.id.trim()).filter(Boolean).map((id) => `plugins.entries.${id}`);
const channelPrefixes = channels.map((channel) => channel.id.trim()).filter(Boolean).map((id) => `channels.${id}`);
const prefixes = [...pluginPrefixes, ...channelPrefixes];
return new Set(Object.keys(hints).filter((key) => prefixes.some((prefix) => key === prefix || key.startsWith(`${prefix}.`))));
}
function applyPluginHints(hints, plugins) {
const next = { ...hints };
for (const plugin of plugins) {
const id = plugin.id.trim();
if (!id) continue;
const name = (plugin.name ?? id).trim() || id;
const basePath = `plugins.entries.${id}`;
next[basePath] = {
...next[basePath],
label: name,
help: plugin.description ? `${plugin.description} (plugin: ${id})` : `Plugin entry for ${id}.`
};
next[`${basePath}.enabled`] = {
...next[`${basePath}.enabled`],
label: `Enable ${name}`
};
next[`${basePath}.config`] = {
...next[`${basePath}.config`],
label: `${name} Config`,
help: `Plugin-defined config payload for ${id}.`
};
const uiHints = plugin.configUiHints ?? {};
for (const [relPathRaw, hint] of Object.entries(uiHints)) {
const relPath = relPathRaw.trim().replace(/^\./, "");
if (!relPath) continue;
const key = `${basePath}.config.${relPath}`;
next[key] = {
...next[key],
...hint
};
}
}
return next;
}
function applyChannelHints(hints, channels) {
const next = { ...hints };
for (const channel of channels) {
const id = channel.id.trim();
if (!id) continue;
const basePath = `channels.${id}`;
const current = next[basePath] ?? {};
const label = channel.label?.trim();
const help = channel.description?.trim();
next[basePath] = {
...current,
...label ? { label } : {},
...help ? { help } : {}
};
const uiHints = channel.configUiHints ?? {};
for (const [relPathRaw, hint] of Object.entries(uiHints)) {
const relPath = relPathRaw.trim().replace(/^\./, "");
if (!relPath) continue;
const key = `${basePath}.${relPath}`;
next[key] = {
...next[key],
...hint
};
}
}
return next;
}
function listHeartbeatTargetChannels(channels) {
const seen = /* @__PURE__ */ new Set();
const ordered = [];
for (const id of CHANNEL_IDS) {
const normalized = id.trim().toLowerCase();
if (!normalized || seen.has(normalized)) continue;
seen.add(normalized);
ordered.push(normalized);
}
for (const channel of channels) {
const normalized = channel.id.trim().toLowerCase();
if (!normalized || seen.has(normalized)) continue;
seen.add(normalized);
ordered.push(normalized);
}
return ordered;
}
function applyHeartbeatTargetHints(hints, channels) {
const next = { ...hints };
const channelList = listHeartbeatTargetChannels(channels);
const help = `Delivery target ("last", "none", or a channel id).${channelList.length ? ` Known channels: ${channelList.join(", ")}.` : ""}`;
for (const path of ["agents.defaults.heartbeat.target", "agents.list.*.heartbeat.target"]) {
const current = next[path] ?? {};
next[path] = {
...current,
help: current.help ?? help,
placeholder: current.placeholder ?? "last"
};
}
return next;
}
function applyPluginSchemas(schema, plugins) {
const next = cloneSchema(schema);
const entriesNode = asSchemaObject(asSchemaObject(asSchemaObject(next)?.properties?.plugins)?.properties?.entries);
if (!entriesNode) return next;
const entryBase = asSchemaObject(entriesNode.additionalProperties);
const entryProperties = entriesNode.properties ?? {};
entriesNode.properties = entryProperties;
for (const plugin of plugins) {
if (!plugin.configSchema) continue;
const entryObject = asSchemaObject(entryBase ? cloneSchema(entryBase) : { type: "object" }) ?? { type: "object" };
const baseConfigSchema = asSchemaObject(entryObject.properties?.config);
const pluginSchema = asSchemaObject(plugin.configSchema);
const nextConfigSchema = baseConfigSchema && pluginSchema && isObjectSchema(baseConfigSchema) && isObjectSchema(pluginSchema) ? mergeObjectSchema(baseConfigSchema, pluginSchema) : cloneSchema(plugin.configSchema);
entryObject.properties = {
...entryObject.properties,
config: nextConfigSchema
};
entryProperties[plugin.id] = entryObject;
}
return next;
}
function applyChannelSchemas(schema, channels) {
const next = cloneSchema(schema);
const channelsNode = asSchemaObject(asSchemaObject(next)?.properties?.channels);
if (!channelsNode) return next;
const channelProps = channelsNode.properties ?? {};
channelsNode.properties = channelProps;
for (const channel of channels) {
if (!channel.configSchema) continue;
const existing = asSchemaObject(channelProps[channel.id]);
const incoming = asSchemaObject(channel.configSchema);
if (existing && incoming && isObjectSchema(existing) && isObjectSchema(incoming)) channelProps[channel.id] = mergeObjectSchema(existing, incoming);
else channelProps[channel.id] = cloneSchema(channel.configSchema);
}
return next;
}
let cachedBase = null;
const mergedSchemaCache = /* @__PURE__ */ new Map();
const MERGED_SCHEMA_CACHE_MAX = 64;
function buildMergedSchemaCacheKey(params) {
const plugins = params.plugins.map((plugin) => ({
id: plugin.id,
name: plugin.name,
description: plugin.description,
configSchema: plugin.configSchema ?? null,
configUiHints: plugin.configUiHints ?? null
})).toSorted((a, b) => a.id.localeCompare(b.id));
const channels = params.channels.map((channel) => ({
id: channel.id,
label: channel.label,
description: channel.description,
configSchema: channel.configSchema ?? null,
configUiHints: channel.configUiHints ?? null
})).toSorted((a, b) => a.id.localeCompare(b.id));
const hash = crypto.createHash("sha256");
hash.update("{\"plugins\":[");
plugins.forEach((plugin, index) => {
if (index > 0) hash.update(",");
hash.update(JSON.stringify(plugin));
});
hash.update("],\"channels\":[");
channels.forEach((channel, index) => {
if (index > 0) hash.update(",");
hash.update(JSON.stringify(channel));
});
hash.update("]}");
return hash.digest("hex");
}
function setMergedSchemaCache(key, value) {
if (mergedSchemaCache.size >= MERGED_SCHEMA_CACHE_MAX) {
const oldest = mergedSchemaCache.keys().next();
if (!oldest.done) mergedSchemaCache.delete(oldest.value);
}
mergedSchemaCache.set(key, value);
}
function buildBaseConfigSchema() {
if (cachedBase) return cachedBase;
const next = GENERATED_BASE_CONFIG_SCHEMA;
cachedBase = next;
return next;
}
function buildConfigSchema(params) {
const base = buildBaseConfigSchema();
const plugins = params?.plugins ?? [];
const channels = params?.channels ?? [];
if (plugins.length === 0 && channels.length === 0) return base;
const cacheKey = params?.cache !== false ? buildMergedSchemaCacheKey({
plugins,
channels
}) : null;
if (cacheKey) {
const cached = mergedSchemaCache.get(cacheKey);
if (cached) return cached;
}
const mergedWithoutSensitiveHints = applyHeartbeatTargetHints(applyChannelHints(applyPluginHints(base.uiHints, plugins), channels), channels);
const mergedHints = applyDerivedTags(applySensitiveHints(mergedWithoutSensitiveHints, collectExtensionHintKeys(mergedWithoutSensitiveHints, plugins, channels)));
const mergedSchema = applyChannelSchemas(applyPluginSchemas(base.schema, plugins), channels);
const merged = {
...base,
schema: mergedSchema,
uiHints: mergedHints
};
if (cacheKey) setMergedSchemaCache(cacheKey, merged);
return merged;
}
function normalizeLookupPath(path) {
return path.trim().replace(/\[(\*|\d*)\]/g, (_match, segment) => `.${segment || "*"}`).replace(/^\.+|\.+$/g, "").replace(/\.+/g, ".");
}
function splitLookupPath(path) {
const normalized = normalizeLookupPath(path);
return normalized ? normalized.split(".").filter(Boolean) : [];
}
function resolveUiHintMatch(uiHints, path) {
return findWildcardHintMatch({
uiHints,
path,
splitPath: splitLookupPath
});
}
function resolveItemsSchema(schema, index) {
if (Array.isArray(schema.items)) {
const entry = index === void 0 ? schema.items.find((candidate) => typeof candidate === "object" && candidate !== null) : schema.items[index];
return entry && typeof entry === "object" ? entry : null;
}
return schema.items && typeof schema.items === "object" ? schema.items : null;
}
function resolveLookupChildSchema(schema, segment) {
if (FORBIDDEN_LOOKUP_SEGMENTS.has(segment)) return null;
const properties = schema.properties;
if (properties && Object.hasOwn(properties, segment)) return asSchemaObject(properties[segment]);
const itemIndex = /^\d+$/.test(segment) ? Number.parseInt(segment, 10) : void 0;
const items = resolveItemsSchema(schema, itemIndex);
if ((segment === "*" || itemIndex !== void 0) && items) return items;
if (schema.additionalProperties && typeof schema.additionalProperties === "object") return schema.additionalProperties;
return null;
}
function stripSchemaForLookup(schema) {
const next = {};
for (const [key, value] of Object.entries(schema)) {
if (LOOKUP_SCHEMA_STRING_KEYS.has(key) && typeof value === "string") {
next[key] = value;
continue;
}
if (LOOKUP_SCHEMA_NUMBER_KEYS.has(key) && typeof value === "number") {
next[key] = value;
continue;
}
if (LOOKUP_SCHEMA_BOOLEAN_KEYS.has(key) && typeof value === "boolean") {
next[key] = value;
continue;
}
if (key === "type") {
if (typeof value === "string") next[key] = value;
else if (Array.isArray(value) && value.every((entry) => typeof entry === "string")) next[key] = [...value];
continue;
}
if (key === "enum" && Array.isArray(value)) {
const entries = value.filter((entry) => entry === null || typeof entry === "string" || typeof entry === "number" || typeof entry === "boolean");
if (entries.length === value.length) next[key] = [...entries];
continue;
}
if (key === "const" && (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean")) next[key] = value;
}
return next;
}
function buildLookupChildren(schema, path, uiHints) {
const children = [];
const required = new Set(schema.required ?? []);
const pushChild = (key, childSchema, isRequired) => {
const childPath = path ? `${path}.${key}` : key;
const resolvedHint = resolveUiHintMatch(uiHints, childPath);
children.push({
key,
path: childPath,
type: childSchema.type,
required: isRequired,
hasChildren: schemaHasChildren(childSchema),
hint: resolvedHint?.hint,
hintPath: resolvedHint?.path
});
};
for (const [key, childSchema] of Object.entries(schema.properties ?? {})) pushChild(key, childSchema, required.has(key));
const wildcardSchema = (schema.additionalProperties && typeof schema.additionalProperties === "object" && !Array.isArray(schema.additionalProperties) ? schema.additionalProperties : null) ?? resolveItemsSchema(schema);
if (wildcardSchema) pushChild("*", wildcardSchema, false);
return children;
}
function lookupConfigSchema(response, path) {
const normalizedPath = normalizeLookupPath(path);
if (!normalizedPath) return null;
const parts = splitLookupPath(normalizedPath);
if (parts.length === 0 || parts.length > MAX_LOOKUP_PATH_SEGMENTS) return null;
let current = asSchemaObject(response.schema);
if (!current) return null;
for (const segment of parts) {
const next = resolveLookupChildSchema(current, segment);
if (!next) return null;
current = next;
}
const resolvedHint = resolveUiHintMatch(response.uiHints, normalizedPath);
return {
path: normalizedPath,
schema: stripSchemaForLookup(current),
hint: resolvedHint?.hint,
hintPath: resolvedHint?.path,
children: buildLookupChildren(current, normalizedPath, response.uiHints)
};
}
//#endregion
//#region src/gateway/server-methods/base-hash.ts
function resolveBaseHashParam(params) {
const raw = params?.baseHash;
if (typeof raw !== "string") return null;
const trimmed = raw.trim();
return trimmed ? trimmed : null;
}
//#endregion
//#region src/gateway/server-methods/restart-request.ts
function parseRestartRequestParams(params) {
const sessionKey = typeof params.sessionKey === "string" ? params.sessionKey?.trim() || void 0 : void 0;
const note = typeof params.note === "string" ? params.note?.trim() || void 0 : void 0;
const restartDelayMsRaw = params.restartDelayMs;
return {
sessionKey,
note,
restartDelayMs: typeof restartDelayMsRaw === "number" && Number.isFinite(restartDelayMsRaw) ? Math.max(0, Math.floor(restartDelayMsRaw)) : void 0
};
}
//#endregion
//#region src/gateway/server-methods/validation.ts
function assertValidParams(params, validate, method, respond) {
if (validate(params)) return true;
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid ${method} params: ${formatValidationErrors(validate.errors)}`));
return false;
}
//#endregion
//#region src/gateway/server-methods/config.ts
const MAX_CONFIG_ISSUES_IN_ERROR_MESSAGE = 3;
function requireConfigBaseHash(params, snapshot, respond) {
if (!snapshot.exists) return true;
const snapshotHash = resolveConfigSnapshotHash(snapshot);
if (!snapshotHash) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "config base hash unavailable; re-run config.get and retry"));
return false;
}
const baseHash = resolveBaseHashParam(params);
if (!baseHash) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "config base hash required; re-run config.get and retry"));
return false;
}
if (baseHash !== snapshotHash) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "config changed since last load; re-run config.get and retry"));
return false;
}
return true;
}
function parseRawConfigOrRespond(params, requestName, respond) {
const rawValue = params.raw;
if (typeof rawValue !== "string") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid ${requestName} params: raw (string) required`));
return null;
}
return rawValue;
}
function sanitizeLookupPathForLog(path) {
const sanitized = Array.from(path, (char) => {
const code = char.charCodeAt(0);
return code < 32 || code === 127 ? "?" : char;
}).join("");
return sanitized.length > 120 ? `${sanitized.slice(0, 117)}...` : sanitized;
}
function parseValidateConfigFromRawOrRespond(params, requestName, snapshot, respond) {
const rawValue = parseRawConfigOrRespond(params, requestName, respond);
if (!rawValue) return null;
const parsedRes = parseConfigJson5(rawValue);
if (!parsedRes.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, parsedRes.error));
return null;
}
const schema = loadSchemaWithPlugins();
const restored = restoreRedactedValues(parsedRes.parsed, snapshot.config, schema.uiHints);
if (!restored.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, restored.humanReadableMessage ?? "invalid config"));
return null;
}
const validated = validateConfigObjectWithPlugins(restored.result);
if (!validated.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, summarizeConfigValidationIssues(validated.issues), { details: { issues: validated.issues } }));
return null;
}
return {
config: validated.config,
schema
};
}
function summarizeConfigValidationIssues(issues) {
const lines = formatConfigIssueLines(issues.slice(0, MAX_CONFIG_ISSUES_IN_ERROR_MESSAGE), "", { normalizeRoot: true }).map((line) => line.trim()).filter(Boolean);
if (lines.length === 0) return "invalid config";
const hiddenCount = Math.max(0, issues.length - lines.length);
return `invalid config: ${lines.join("; ")}${hiddenCount > 0 ? ` (+${hiddenCount} more issue${hiddenCount === 1 ? "" : "s"})` : ""}`;
}
function resolveConfigRestartRequest(params) {
const { sessionKey, note, restartDelayMs } = parseRestartRequestParams(params);
const { deliveryContext, threadId } = extractDeliveryInfo(sessionKey);
return {
sessionKey,
note,
restartDelayMs,
deliveryContext,
threadId
};
}
function buildConfigRestartSentinelPayload(params) {
const configPath = createConfigIO().configPath;
return {
kind: params.kind,
status: "ok",
ts: Date.now(),
sessionKey: params.sessionKey,
deliveryContext: params.deliveryContext,
threadId: params.threadId,
message: params.note ?? null,
doctorHint: formatDoctorNonInteractiveHint(),
stats: {
mode: params.mode,
root: configPath
}
};
}
async function tryWriteRestartSentinelPayload(payload) {
try {
return await writeRestartSentinel(payload);
} catch {
return null;
}
}
function loadSchemaWithPlugins() {
const cfg = loadConfig();
return buildConfigSchema({
plugins: loadOpenClawPlugins({
config: cfg,
cache: true,
workspaceDir: resolveAgentWorkspaceDir(cfg, resolveDefaultAgentId(cfg)),
runtimeOptions: { allowGatewaySubagentBinding: true },
logger: {
info: () => {},
warn: () => {},
error: () => {},
debug: () => {}
}
}).plugins.map((plugin) => ({
id: plugin.id,
name: plugin.name,
description: plugin.description,
configUiHints: plugin.configUiHints,
configSchema: plugin.configJsonSchema
})),
channels: listChannelPlugins().map((entry) => ({
id: entry.id,
label: entry.meta.label,
description: entry.meta.blurb,
configSchema: entry.configSchema?.schema,
configUiHints: entry.configSchema?.uiHints
}))
});
}
const configHandlers = {
"config.get": async ({ params, respond }) => {
if (!assertValidParams(params, validateConfigGetParams, "config.get", respond)) return;
respond(true, redactConfigSnapshot(await readConfigFileSnapshot(), loadSchemaWithPlugins().uiHints), void 0);
},
"config.schema": ({ params, respond }) => {
if (!assertValidParams(params, validateConfigSchemaParams, "config.schema", respond)) return;
respond(true, loadSchemaWithPlugins(), void 0);
},
"config.schema.lookup": ({ params, respond, context }) => {
if (!assertValidParams(params, validateConfigSchemaLookupParams, "config.schema.lookup", respond)) return;
const path = params.path;
const result = lookupConfigSchema(loadSchemaWithPlugins(), path);
if (!result) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "config schema path not found"));
return;
}
if (!validateConfigSchemaLookupResult(result)) {
const errors = validateConfigSchemaLookupResult.errors ?? [];
context.logGateway.warn(`config.schema.lookup produced invalid payload for ${sanitizeLookupPathForLog(path)}: ${formatValidationErrors(errors)}`);
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "config.schema.lookup returned invalid payload", { details: { errors } }));
return;
}
respond(true, result, void 0);
},
"config.set": async ({ params, respond }) => {
if (!assertValidParams(params, validateConfigSetParams, "config.set", respond)) return;
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
if (!requireConfigBaseHash(params, snapshot, respond)) return;
const parsed = parseValidateConfigFromRawOrRespond(params, "config.set", snapshot, respond);
if (!parsed) return;
await writeConfigFile(parsed.config, writeOptions);
respond(true, {
ok: true,
path: createConfigIO().configPath,
config: redactConfigObject(parsed.config, parsed.schema.uiHints)
}, void 0);
},
"config.patch": async ({ params, respond, client, context }) => {
if (!assertValidParams(params, validateConfigPatchParams, "config.patch", respond)) return;
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
if (!requireConfigBaseHash(params, snapshot, respond)) return;
if (!snapshot.valid) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid config; fix before patching"));
return;
}
const rawValue = params.raw;
if (typeof rawValue !== "string") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid config.patch params: raw (string) required"));
return;
}
const parsedRes = parseConfigJson5(rawValue);
if (!parsedRes.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, parsedRes.error));
return;
}
if (!parsedRes.parsed || typeof parsedRes.parsed !== "object" || Array.isArray(parsedRes.parsed)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "config.patch raw must be an object"));
return;
}
const merged = applyMergePatch(snapshot.config, parsedRes.parsed, { mergeObjectArraysById: true });
const schemaPatch = loadSchemaWithPlugins();
const restoredMerge = restoreRedactedValues(merged, snapshot.config, schemaPatch.uiHints);
if (!restoredMerge.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, restoredMerge.humanReadableMessage ?? "invalid config"));
return;
}
const validated = validateConfigObjectWithPlugins(applyLegacyMigrations(restoredMerge.result).next ?? restoredMerge.result);
if (!validated.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, summarizeConfigValidationIssues(validated.issues), { details: { issues: validated.issues } }));
return;
}
const changedPaths = diffConfigPaths(snapshot.config, validated.config);
const actor = resolveControlPlaneActor(client);
context?.logGateway?.info(`config.patch write ${formatControlPlaneActor(actor)} changedPaths=${summarizeChangedPaths(changedPaths)} restartReason=config.patch`);
await writeConfigFile(validated.config, writeOptions);
const { sessionKey, note, restartDelayMs, deliveryContext, threadId } = resolveConfigRestartRequest(params);
const payload = buildConfigRestartSentinelPayload({
kind: "config-patch",
mode: "config.patch",
sessionKey,
deliveryContext,
threadId,
note
});
const sentinelPath = await tryWriteRestartSentinelPayload(payload);
const restart = scheduleGatewaySigusr1Restart({
delayMs: restartDelayMs,
reason: "config.patch",
audit: {
actor: actor.actor,
deviceId: actor.deviceId,
clientIp: actor.clientIp,
changedPaths
}
});
if (restart.coalesced) context?.logGateway?.warn(`config.patch restart coalesced ${formatControlPlaneActor(actor)} delayMs=${restart.delayMs}`);
respond(true, {
ok: true,
path: createConfigIO().configPath,
config: redactConfigObject(validated.config, schemaPatch.uiHints),
restart,
sentinel: {
path: sentinelPath,
payload
}
}, void 0);
},
"config.apply": async ({ params, respond, client, context }) => {
if (!assertValidParams(params, validateConfigApplyParams, "config.apply", respond)) return;
const { snapshot, writeOptions } = await readConfigFileSnapshotForWrite();
if (!requireConfigBaseHash(params, snapshot, respond)) return;
const parsed = parseValidateConfigFromRawOrRespond(params, "config.apply", snapshot, respond);
if (!parsed) return;
const changedPaths = diffConfigPaths(snapshot.config, parsed.config);
const actor = resolveControlPlaneActor(client);
context?.logGateway?.info(`config.apply write ${formatControlPlaneActor(actor)} changedPaths=${summarizeChangedPaths(changedPaths)} restartReason=config.apply`);
await writeConfigFile(parsed.config, writeOptions);
const { sessionKey, note, restartDelayMs, deliveryContext, threadId } = resolveConfigRestartRequest(params);
const payload = buildConfigRestartSentinelPayload({
kind: "config-apply",
mode: "config.apply",
sessionKey,
deliveryContext,
threadId,
note
});
const sentinelPath = await tryWriteRestartSentinelPayload(payload);
const restart = scheduleGatewaySigusr1Restart({
delayMs: restartDelayMs,
reason: "config.apply",
audit: {
actor: actor.actor,
deviceId: actor.deviceId,
clientIp: actor.clientIp,
changedPaths
}
});
if (restart.coalesced) context?.logGateway?.warn(`config.apply restart coalesced ${formatControlPlaneActor(actor)} delayMs=${restart.delayMs}`);
respond(true, {
ok: true,
path: createConfigIO().configPath,
config: redactConfigObject(parsed.config, parsed.schema.uiHints),
restart,
sentinel: {
path: sentinelPath,
payload
}
}, void 0);
},
"config.openFile": ({ params, respond }) => {
if (!assertValidParams(params, validateConfigGetParams, "config.openFile", respond)) return;
const configPath = createConfigIO().configPath;
const platform = process.platform;
exec(`${platform === "darwin" ? "open" : platform === "win32" ? "start" : "xdg-open"} ${JSON.stringify(configPath)}`, (err) => {
if (err) {
respond(true, {
ok: false,
path: configPath,
error: err.message
}, void 0);
return;
}
respond(true, {
ok: true,
path: configPath
}, void 0);
});
}
};
//#endregion
//#region src/gateway/server-methods/connect.ts
const connectHandlers = { connect: ({ respond }) => {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "connect is only valid as the first request"));
} };
//#endregion
//#region src/cron/validate-timestamp.ts
const ONE_MINUTE_MS = 60 * 1e3;
const TEN_YEARS_MS = 10 * 365.25 * 24 * 60 * 60 * 1e3;
/**
* Validates at timestamps in cron schedules.
* Rejects timestamps that are:
* - More than 1 minute in the past
* - More than 10 years in the future
*/
function validateScheduleTimestamp(schedule, nowMs = Date.now()) {
if (schedule.kind !== "at") return { ok: true };
const atRaw = typeof schedule.at === "string" ? schedule.at.trim() : "";
const atMs = atRaw ? parseAbsoluteTimeMs(atRaw) : null;
if (atMs === null || !Number.isFinite(atMs)) return {
ok: false,
message: `Invalid schedule.at: expected ISO-8601 timestamp (got ${String(schedule.at)})`
};
const diffMs = atMs - nowMs;
if (diffMs < -ONE_MINUTE_MS) {
const nowDate = new Date(nowMs).toISOString();
return {
ok: false,
message: `schedule.at is in the past: ${new Date(atMs).toISOString()} (${Math.floor(-diffMs / ONE_MINUTE_MS)} minutes ago). Current time: ${nowDate}`
};
}
if (diffMs > TEN_YEARS_MS) return {
ok: false,
message: `schedule.at is too far in the future: ${new Date(atMs).toISOString()} (${Math.floor(diffMs / (365.25 * 24 * 60 * 60 * 1e3))} years ahead). Maximum allowed: 10 years`
};
return { ok: true };
}
//#endregion
//#region src/gateway/server-methods/cron.ts
const cronHandlers = {
wake: ({ params, respond, context }) => {
if (!validateWakeParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid wake params: ${formatValidationErrors(validateWakeParams.errors)}`));
return;
}
const p = params;
respond(true, context.cron.wake({
mode: p.mode,
text: p.text
}), void 0);
},
"cron.list": async ({ params, respond, context }) => {
if (!validateCronListParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.list params: ${formatValidationErrors(validateCronListParams.errors)}`));
return;
}
const p = params;
respond(true, await context.cron.listPage({
includeDisabled: p.includeDisabled,
limit: p.limit,
offset: p.offset,
query: p.query,
enabled: p.enabled,
sortBy: p.sortBy,
sortDir: p.sortDir
}), void 0);
},
"cron.status": async ({ params, respond, context }) => {
if (!validateCronStatusParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.status params: ${formatValidationErrors(validateCronStatusParams.errors)}`));
return;
}
respond(true, await context.cron.status(), void 0);
},
"cron.add": async ({ params, respond, context }) => {
const normalized = normalizeCronJobCreate(params, { sessionContext: { sessionKey: typeof params?.sessionKey === "string" ? params.sessionKey : void 0 } }) ?? params;
if (!validateCronAddParams(normalized)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.add params: ${formatValidationErrors(validateCronAddParams.errors)}`));
return;
}
const jobCreate = normalized;
const timestampValidation = validateScheduleTimestamp(jobCreate.schedule);
if (!timestampValidation.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, timestampValidation.message));
return;
}
const job = await context.cron.add(jobCreate);
context.logGateway.info("cron: job created", {
jobId: job.id,
schedule: jobCreate.schedule
});
respond(true, job, void 0);
},
"cron.update": async ({ params, respond, context }) => {
const normalizedPatch = normalizeCronJobPatch(params?.patch);
const candidate = normalizedPatch && typeof params === "object" && params !== null ? {
...params,
patch: normalizedPatch
} : params;
if (!validateCronUpdateParams(candidate)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.update params: ${formatValidationErrors(validateCronUpdateParams.errors)}`));
return;
}
const p = candidate;
const jobId = p.id ?? p.jobId;
if (!jobId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid cron.update params: missing id"));
return;
}
const patch = p.patch;
if (patch.schedule) {
const timestampValidation = validateScheduleTimestamp(patch.schedule);
if (!timestampValidation.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, timestampValidation.message));
return;
}
}
const job = await context.cron.update(jobId, patch);
context.logGateway.info("cron: job updated", { jobId });
respond(true, job, void 0);
},
"cron.remove": async ({ params, respond, context }) => {
if (!validateCronRemoveParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.remove params: ${formatValidationErrors(validateCronRemoveParams.errors)}`));
return;
}
const p = params;
const jobId = p.id ?? p.jobId;
if (!jobId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid cron.remove params: missing id"));
return;
}
const result = await context.cron.remove(jobId);
if (result.removed) context.logGateway.info("cron: job removed", { jobId });
respond(true, result, void 0);
},
"cron.run": async ({ params, respond, context }) => {
if (!validateCronRunParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.run params: ${formatValidationErrors(validateCronRunParams.errors)}`));
return;
}
const p = params;
const jobId = p.id ?? p.jobId;
if (!jobId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid cron.run params: missing id"));
return;
}
respond(true, await context.cron.enqueueRun(jobId, p.mode ?? "force"), void 0);
},
"cron.runs": async ({ params, respond, context }) => {
if (!validateCronRunsParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid cron.runs params: ${formatValidationErrors(validateCronRunsParams.errors)}`));
return;
}
const p = params;
const explicitScope = p.scope;
const jobId = p.id ?? p.jobId;
const scope = explicitScope ?? (jobId ? "job" : "all");
if (scope === "job" && !jobId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid cron.runs params: missing id"));
return;
}
if (scope === "all") {
const jobs = await context.cron.list({ includeDisabled: true });
const jobNameById = Object.fromEntries(jobs.filter((job) => typeof job.id === "string" && typeof job.name === "string").map((job) => [job.id, job.name]));
respond(true, await readCronRunLogEntriesPageAll({
storePath: context.cronStorePath,
limit: p.limit,
offset: p.offset,
statuses: p.statuses,
status: p.status,
deliveryStatuses: p.deliveryStatuses,
deliveryStatus: p.deliveryStatus,
query: p.query,
sortDir: p.sortDir,
jobNameById
}), void 0);
return;
}
let logPath;
try {
logPath = resolveCronRunLogPath({
storePath: context.cronStorePath,
jobId
});
} catch {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid cron.runs params: invalid id"));
return;
}
respond(true, await readCronRunLogEntriesPage(logPath, {
limit: p.limit,
offset: p.offset,
jobId,
statuses: p.statuses,
status: p.status,
deliveryStatuses: p.deliveryStatuses,
deliveryStatus: p.deliveryStatus,
query: p.query,
sortDir: p.sortDir
}), void 0);
}
};
//#endregion
//#region src/gateway/server-methods/devices.ts
const DEVICE_TOKEN_ROTATION_DENIED_MESSAGE = "device token rotation denied";
function redactPairedDevice(device) {
const { tokens, approvedScopes: _approvedScopes, ...rest } = device;
return {
...rest,
tokens: summarizeDeviceTokens(tokens)
};
}
function logDeviceTokenRotationDenied(params) {
const suffix = params.scope ? ` scope=${params.scope}` : "";
params.log.warn(`device token rotation denied device=${params.deviceId} role=${params.role} reason=${params.reason}${suffix}`);
}
const deviceHandlers = {
"device.pair.list": async ({ params, respond }) => {
if (!validateDevicePairListParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid device.pair.list params: ${formatValidationErrors(validateDevicePairListParams.errors)}`));
return;
}
const list = await listDevicePairing();
respond(true, {
pending: list.pending,
paired: list.paired.map((device) => redactPairedDevice(device))
}, void 0);
},
"device.pair.approve": async ({ params, respond, context, client }) => {
if (!validateDevicePairApproveParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid device.pair.approve params: ${formatValidationErrors(validateDevicePairApproveParams.errors)}`));
return;
}
const { requestId } = params;
const approved = await approveDevicePairing(requestId, { callerScopes: Array.isArray(client?.connect?.scopes) ? client.connect.scopes : [] });
if (!approved) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown requestId"));
return;
}
if (approved.status === "forbidden") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `missing scope: ${approved.missingScope}`));
return;
}
context.logGateway.info(`device pairing approved device=${approved.device.deviceId} role=${approved.device.role ?? "unknown"}`);
context.broadcast("device.pair.resolved", {
requestId,
deviceId: approved.device.deviceId,
decision: "approved",
ts: Date.now()
}, { dropIfSlow: true });
respond(true, {
requestId,
device: redactPairedDevice(approved.device)
}, void 0);
},
"device.pair.reject": async ({ params, respond, context }) => {
if (!validateDevicePairRejectParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid device.pair.reject params: ${formatValidationErrors(validateDevicePairRejectParams.errors)}`));
return;
}
const { requestId } = params;
const rejected = await rejectDevicePairing(requestId);
if (!rejected) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown requestId"));
return;
}
context.broadcast("device.pair.resolved", {
requestId,
deviceId: rejected.deviceId,
decision: "rejected",
ts: Date.now()
}, { dropIfSlow: true });
respond(true, rejected, void 0);
},
"device.pair.remove": async ({ params, respond, context }) => {
if (!validateDevicePairRemoveParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid device.pair.remove params: ${formatValidationErrors(validateDevicePairRemoveParams.errors)}`));
return;
}
const { deviceId } = params;
const removed = await removePairedDevice(deviceId);
if (!removed) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown deviceId"));
return;
}
context.logGateway.info(`device pairing removed device=${removed.deviceId}`);
respond(true, removed, void 0);
},
"device.token.rotate": async ({ params, respond, context, client }) => {
if (!validateDeviceTokenRotateParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid device.token.rotate params: ${formatValidationErrors(validateDeviceTokenRotateParams.errors)}`));
return;
}
const { deviceId, role, scopes } = params;
const pairedDevice = await getPairedDevice(deviceId);
if (!pairedDevice) {
logDeviceTokenRotationDenied({
log: context.logGateway,
deviceId,
role,
reason: "unknown-device-or-role"
});
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, DEVICE_TOKEN_ROTATION_DENIED_MESSAGE));
return;
}
const callerScopes = Array.isArray(client?.connect?.scopes) ? client.connect.scopes : [];
const missingScope = resolveMissingRequestedScope({
role,
requestedScopes: normalizeDeviceAuthScopes(scopes ?? pairedDevice.tokens?.[role.trim()]?.scopes ?? pairedDevice.scopes),
allowedScopes: callerScopes
});
if (missingScope) {
logDeviceTokenRotationDenied({
log: context.logGateway,
deviceId,
role,
reason: "caller-missing-scope",
scope: missingScope
});
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, DEVICE_TOKEN_ROTATION_DENIED_MESSAGE));
return;
}
const rotated = await rotateDeviceToken({
deviceId,
role,
scopes
});
if (!rotated.ok) {
logDeviceTokenRotationDenied({
log: context.logGateway,
deviceId,
role,
reason: rotated.reason
});
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, DEVICE_TOKEN_ROTATION_DENIED_MESSAGE));
return;
}
const entry = rotated.entry;
context.logGateway.info(`device token rotated device=${deviceId} role=${entry.role} scopes=${entry.scopes.join(",")}`);
respond(true, {
deviceId,
role: entry.role,
token: entry.token,
scopes: entry.scopes,
rotatedAtMs: entry.rotatedAtMs ?? entry.createdAtMs
}, void 0);
},
"device.token.revoke": async ({ params, respond, context }) => {
if (!validateDeviceTokenRevokeParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid device.token.revoke params: ${formatValidationErrors(validateDeviceTokenRevokeParams.errors)}`));
return;
}
const { deviceId, role } = params;
const entry = await revokeDeviceToken({
deviceId,
role
});
if (!entry) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown deviceId/role"));
return;
}
context.logGateway.info(`device token revoked device=${deviceId} role=${entry.role}`);
respond(true, {
deviceId,
role: entry.role,
revokedAtMs: entry.revokedAtMs ?? Date.now()
}, void 0);
}
};
//#endregion
//#region src/gateway/server-methods/doctor.ts
const doctorHandlers = { "doctor.memory.status": async ({ respond }) => {
const cfg = loadConfig();
const agentId = resolveDefaultAgentId(cfg);
const { manager, error } = await getMemorySearchManager({
cfg,
agentId,
purpose: "status"
});
if (!manager) {
respond(true, {
agentId,
embedding: {
ok: false,
error: error ?? "memory search unavailable"
}
}, void 0);
return;
}
try {
const status = manager.status();
let embedding = await manager.probeEmbeddingAvailability();
if (!embedding.ok && !embedding.error) embedding = {
ok: false,
error: "memory embeddings unavailable"
};
respond(true, {
agentId,
provider: status.provider,
embedding
}, void 0);
} catch (err) {
respond(true, {
agentId,
embedding: {
ok: false,
error: `gateway memory probe failed: ${formatError(err)}`
}
}, void 0);
} finally {
await manager.close?.().catch(() => {});
}
} };
//#endregion
//#region src/gateway/server-methods/exec-approvals.ts
function requireApprovalsBaseHash(params, snapshot, respond) {
if (!snapshot.exists) return true;
if (!snapshot.hash) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "exec approvals base hash unavailable; re-run exec.approvals.get and retry"));
return false;
}
const baseHash = resolveBaseHashParam(params);
if (!baseHash) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "exec approvals base hash required; re-run exec.approvals.get and retry"));
return false;
}
if (baseHash !== snapshot.hash) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "exec approvals changed since last load; re-run exec.approvals.get and retry"));
return false;
}
return true;
}
function redactExecApprovals(file) {
const socketPath = file.socket?.path?.trim();
return {
...file,
socket: socketPath ? { path: socketPath } : void 0
};
}
function toExecApprovalsPayload(snapshot) {
return {
path: snapshot.path,
exists: snapshot.exists,
hash: snapshot.hash,
file: redactExecApprovals(snapshot.file)
};
}
function resolveNodeIdOrRespond(nodeId, respond) {
const id = nodeId.trim();
if (!id) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId required"));
return null;
}
return id;
}
const execApprovalsHandlers = {
"exec.approvals.get": ({ params, respond }) => {
if (!assertValidParams(params, validateExecApprovalsGetParams, "exec.approvals.get", respond)) return;
ensureExecApprovals();
respond(true, toExecApprovalsPayload(readExecApprovalsSnapshot()), void 0);
},
"exec.approvals.set": ({ params, respond }) => {
if (!assertValidParams(params, validateExecApprovalsSetParams, "exec.approvals.set", respond)) return;
ensureExecApprovals();
const snapshot = readExecApprovalsSnapshot();
if (!requireApprovalsBaseHash(params, snapshot, respond)) return;
const incoming = params.file;
if (!incoming || typeof incoming !== "object") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "exec approvals file is required"));
return;
}
saveExecApprovals(mergeExecApprovalsSocketDefaults({
normalized: normalizeExecApprovals(incoming),
current: snapshot.file
}));
respond(true, toExecApprovalsPayload(readExecApprovalsSnapshot()), void 0);
},
"exec.approvals.node.get": async ({ params, respond, context }) => {
if (!assertValidParams(params, validateExecApprovalsNodeGetParams, "exec.approvals.node.get", respond)) return;
const { nodeId } = params;
const id = resolveNodeIdOrRespond(nodeId, respond);
if (!id) return;
await respondUnavailableOnThrow(respond, async () => {
const res = await context.nodeRegistry.invoke({
nodeId: id,
command: "system.execApprovals.get",
params: {}
});
if (!respondUnavailableOnNodeInvokeError(respond, res)) return;
respond(true, res.payloadJSON ? safeParseJson(res.payloadJSON) : res.payload, void 0);
});
},
"exec.approvals.node.set": async ({ params, respond, context }) => {
if (!assertValidParams(params, validateExecApprovalsNodeSetParams, "exec.approvals.node.set", respond)) return;
const { nodeId, file, baseHash } = params;
const id = resolveNodeIdOrRespond(nodeId, respond);
if (!id) return;
await respondUnavailableOnThrow(respond, async () => {
const res = await context.nodeRegistry.invoke({
nodeId: id,
command: "system.execApprovals.set",
params: {
file,
baseHash
}
});
if (!respondUnavailableOnNodeInvokeError(respond, res)) return;
respond(true, safeParseJson(res.payloadJSON ?? null), void 0);
});
}
};
//#endregion
//#region src/gateway/server-methods/health.ts
const ADMIN_SCOPE$1 = "operator.admin";
const healthHandlers = {
health: async ({ respond, context, params }) => {
const { getHealthCache, refreshHealthSnapshot, logHealth } = context;
const wantsProbe = params?.probe === true;
const now = Date.now();
const cached = getHealthCache();
if (!wantsProbe && cached && now - cached.ts < 6e4) {
respond(true, cached, void 0, { cached: true });
refreshHealthSnapshot({ probe: false }).catch((err) => logHealth.error(`background health refresh failed: ${formatError(err)}`));
return;
}
try {
respond(true, await refreshHealthSnapshot({ probe: wantsProbe }), void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
status: async ({ respond, client }) => {
respond(true, await getStatusSummary({ includeSensitive: (Array.isArray(client?.connect?.scopes) ? client.connect.scopes : []).includes(ADMIN_SCOPE$1) }), void 0);
}
};
//#endregion
//#region src/gateway/server-methods/logs.ts
const DEFAULT_LIMIT = 500;
const DEFAULT_MAX_BYTES = 25e4;
const MAX_LIMIT = 5e3;
const MAX_BYTES = 1e6;
const ROLLING_LOG_RE = /^openclaw-\d{4}-\d{2}-\d{2}\.log$/;
function isRollingLogFile(file) {
return ROLLING_LOG_RE.test(path.basename(file));
}
async function resolveLogFile(file) {
if (await fs$1.stat(file).catch(() => null)) return file;
if (!isRollingLogFile(file)) return file;
const dir = path.dirname(file);
const entries = await fs$1.readdir(dir, { withFileTypes: true }).catch(() => null);
if (!entries) return file;
return (await Promise.all(entries.filter((entry) => entry.isFile() && ROLLING_LOG_RE.test(entry.name)).map(async (entry) => {
const fullPath = path.join(dir, entry.name);
const fileStat = await fs$1.stat(fullPath).catch(() => null);
return fileStat ? {
path: fullPath,
mtimeMs: fileStat.mtimeMs
} : null;
}))).filter((entry) => Boolean(entry)).toSorted((a, b) => b.mtimeMs - a.mtimeMs)[0]?.path ?? file;
}
async function readLogSlice(params) {
const stat = await fs$1.stat(params.file).catch(() => null);
if (!stat) return {
cursor: 0,
size: 0,
lines: [],
truncated: false,
reset: false
};
const size = stat.size;
const maxBytes = clamp(params.maxBytes, 1, MAX_BYTES);
const limit = clamp(params.limit, 1, MAX_LIMIT);
let cursor = typeof params.cursor === "number" && Number.isFinite(params.cursor) ? Math.max(0, Math.floor(params.cursor)) : void 0;
let reset = false;
let truncated = false;
let start = 0;
if (cursor != null) if (cursor > size) {
reset = true;
start = Math.max(0, size - maxBytes);
truncated = start > 0;
} else {
start = cursor;
if (size - start > maxBytes) {
reset = true;
truncated = true;
start = Math.max(0, size - maxBytes);
}
}
else {
start = Math.max(0, size - maxBytes);
truncated = start > 0;
}
if (size === 0 || size <= start) return {
cursor: size,
size,
lines: [],
truncated,
reset
};
const handle = await fs$1.open(params.file, "r");
try {
let prefix = "";
if (start > 0) {
const prefixBuf = Buffer.alloc(1);
const prefixRead = await handle.read(prefixBuf, 0, 1, start - 1);
prefix = prefixBuf.toString("utf8", 0, prefixRead.bytesRead);
}
const length = Math.max(0, size - start);
const buffer = Buffer.alloc(length);
const readResult = await handle.read(buffer, 0, length, start);
let lines = buffer.toString("utf8", 0, readResult.bytesRead).split("\n");
if (start > 0 && prefix !== "\n") lines = lines.slice(1);
if (lines.length > 0 && lines[lines.length - 1] === "") lines = lines.slice(0, -1);
if (lines.length > limit) lines = lines.slice(lines.length - limit);
cursor = size;
return {
cursor,
size,
lines,
truncated,
reset
};
} finally {
await handle.close();
}
}
const logsHandlers = { "logs.tail": async ({ params, respond }) => {
if (!validateLogsTailParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid logs.tail params: ${formatValidationErrors(validateLogsTailParams.errors)}`));
return;
}
const p = params;
const configuredFile = getResolvedLoggerSettings().file;
try {
const file = await resolveLogFile(configuredFile);
respond(true, {
file,
...await readLogSlice({
file,
cursor: p.cursor,
limit: p.limit ?? DEFAULT_LIMIT,
maxBytes: p.maxBytes ?? DEFAULT_MAX_BYTES
})
}, void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, `log read failed: ${String(err)}`));
}
} };
//#endregion
//#region src/gateway/server-methods/models.ts
const modelsHandlers = { "models.list": async ({ params, respond, context }) => {
if (!validateModelsListParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid models.list params: ${formatValidationErrors(validateModelsListParams.errors)}`));
return;
}
try {
const catalog = await context.loadGatewayModelCatalog();
const { allowedCatalog } = buildAllowedModelSet({
cfg: loadConfig(),
catalog,
defaultProvider: DEFAULT_PROVIDER
});
respond(true, { models: allowedCatalog.length > 0 ? allowedCatalog : catalog }, void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
}
} };
//#endregion
//#region src/gateway/node-pending-work.ts
const DEFAULT_STATUS_ITEM_ID = "baseline-status";
const DEFAULT_STATUS_PRIORITY = "default";
const DEFAULT_PRIORITY = "normal";
const DEFAULT_MAX_ITEMS = 4;
const MAX_ITEMS = 10;
const PRIORITY_RANK = {
high: 3,
normal: 2,
default: 1
};
const stateByNodeId = /* @__PURE__ */ new Map();
function getOrCreateState(nodeId) {
let state = stateByNodeId.get(nodeId);
if (!state) {
state = {
revision: 0,
itemsById: /* @__PURE__ */ new Map()
};
stateByNodeId.set(nodeId, state);
}
return state;
}
function pruneExpired(state, nowMs) {
let changed = false;
for (const [id, item] of state.itemsById) if (item.expiresAtMs !== null && item.expiresAtMs <= nowMs) {
state.itemsById.delete(id);
changed = true;
}
if (changed) state.revision += 1;
return changed;
}
function sortedItems(state) {
return [...state.itemsById.values()].toSorted((a, b) => {
const priorityDelta = PRIORITY_RANK[b.priority] - PRIORITY_RANK[a.priority];
if (priorityDelta !== 0) return priorityDelta;
if (a.createdAtMs !== b.createdAtMs) return a.createdAtMs - b.createdAtMs;
return a.id.localeCompare(b.id);
});
}
function makeBaselineStatusItem(nowMs) {
return {
id: DEFAULT_STATUS_ITEM_ID,
type: "status.request",
priority: DEFAULT_STATUS_PRIORITY,
createdAtMs: nowMs,
expiresAtMs: null
};
}
function enqueueNodePendingWork(params) {
const nodeId = params.nodeId.trim();
if (!nodeId) throw new Error("nodeId required");
const nowMs = Date.now();
const state = getOrCreateState(nodeId);
pruneExpired(state, nowMs);
const existing = [...state.itemsById.values()].find((item) => item.type === params.type);
if (existing) return {
revision: state.revision,
item: existing,
deduped: true
};
const item = {
id: randomUUID(),
type: params.type,
priority: params.priority ?? DEFAULT_PRIORITY,
createdAtMs: nowMs,
expiresAtMs: typeof params.expiresInMs === "number" && Number.isFinite(params.expiresInMs) ? nowMs + Math.max(1e3, Math.trunc(params.expiresInMs)) : null,
...params.payload ? { payload: params.payload } : {}
};
state.itemsById.set(item.id, item);
state.revision += 1;
return {
revision: state.revision,
item,
deduped: false
};
}
function drainNodePendingWork(nodeId, opts = {}) {
const normalizedNodeId = nodeId.trim();
if (!normalizedNodeId) return {
revision: 0,
items: [],
hasMore: false
};
const nowMs = opts.nowMs ?? Date.now();
const state = stateByNodeId.get(normalizedNodeId);
const revision = state?.revision ?? 0;
if (state) pruneExpired(state, nowMs);
const maxItems = Math.min(MAX_ITEMS, Math.max(1, Math.trunc(opts.maxItems ?? DEFAULT_MAX_ITEMS)));
const explicitItems = state ? sortedItems(state) : [];
const items = explicitItems.slice(0, maxItems);
const hasExplicitStatus = explicitItems.some((item) => item.type === "status.request");
const includeBaseline = opts.includeDefaultStatus !== false && !hasExplicitStatus;
if (includeBaseline && items.length < maxItems) items.push(makeBaselineStatusItem(nowMs));
const explicitReturnedCount = items.filter((item) => item.id !== DEFAULT_STATUS_ITEM_ID).length;
const baselineIncluded = items.some((item) => item.id === DEFAULT_STATUS_ITEM_ID);
return {
revision,
items,
hasMore: explicitItems.length > explicitReturnedCount || includeBaseline && !baselineIncluded
};
}
//#endregion
//#region src/gateway/canvas-capability.ts
const CANVAS_CAPABILITY_PATH_PREFIX = "/__openclaw__/cap";
const CANVAS_CAPABILITY_QUERY_PARAM = "oc_cap";
const CANVAS_CAPABILITY_TTL_MS = 10 * 6e4;
function normalizeCapability(raw) {
const trimmed = raw?.trim();
return trimmed ? trimmed : void 0;
}
function mintCanvasCapabilityToken() {
return randomBytes(18).toString("base64url");
}
function buildCanvasScopedHostUrl(baseUrl, capability) {
const normalizedCapability = normalizeCapability(capability);
if (!normalizedCapability) return;
try {
const url = new URL(baseUrl);
url.pathname = `${url.pathname.replace(/\/+$/, "")}${`${CANVAS_CAPABILITY_PATH_PREFIX}/${encodeURIComponent(normalizedCapability)}`}`;
url.search = "";
url.hash = "";
return url.toString().replace(/\/$/, "");
} catch {
return;
}
}
function normalizeCanvasScopedUrl(rawUrl) {
const url = new URL(rawUrl, "http://localhost");
const prefix = `${CANVAS_CAPABILITY_PATH_PREFIX}/`;
let scopedPath = false;
let malformedScopedPath = false;
let capabilityFromPath;
let rewrittenUrl;
if (url.pathname.startsWith(prefix)) {
scopedPath = true;
const remainder = url.pathname.slice(prefix.length);
const slashIndex = remainder.indexOf("/");
if (slashIndex <= 0) malformedScopedPath = true;
else {
const encodedCapability = remainder.slice(0, slashIndex);
const canonicalPath = remainder.slice(slashIndex) || "/";
let decoded;
try {
decoded = decodeURIComponent(encodedCapability);
} catch {
malformedScopedPath = true;
}
capabilityFromPath = normalizeCapability(decoded);
if (!capabilityFromPath || !canonicalPath.startsWith("/")) malformedScopedPath = true;
else {
url.pathname = canonicalPath;
if (!url.searchParams.has("oc_cap")) url.searchParams.set(CANVAS_CAPABILITY_QUERY_PARAM, capabilityFromPath);
rewrittenUrl = `${url.pathname}${url.search}`;
}
}
}
const capability = capabilityFromPath ?? normalizeCapability(url.searchParams.get("oc_cap"));
return {
pathname: url.pathname,
capability,
rewrittenUrl,
scopedPath,
malformedScopedPath
};
}
//#endregion
//#region src/gateway/node-invoke-system-run-approval-errors.ts
function systemRunApprovalGuardError(params) {
const details = params.details ? { ...params.details } : {};
return {
ok: false,
message: params.message,
details: {
code: params.code,
...details
}
};
}
function systemRunApprovalRequired(runId) {
return systemRunApprovalGuardError({
code: "APPROVAL_REQUIRED",
message: "approval required",
details: { runId }
});
}
//#endregion
//#region src/gateway/node-invoke-system-run-approval-match.ts
function requestMismatch() {
return {
ok: false,
code: "APPROVAL_REQUEST_MISMATCH",
message: "approval id does not match request"
};
}
function evaluateSystemRunApprovalMatch(params) {
if (params.request.host !== "node") return requestMismatch();
const actualBinding = buildSystemRunApprovalBinding({
argv: params.argv,
cwd: params.binding.cwd,
agentId: params.binding.agentId,
sessionKey: params.binding.sessionKey,
env: params.binding.env
});
const expectedBinding = params.request.systemRunBinding;
if (!expectedBinding) return missingSystemRunApprovalBinding({ actualEnvKeys: actualBinding.envKeys });
return matchSystemRunApprovalBinding({
expected: expectedBinding,
actual: actualBinding.binding,
actualEnvKeys: actualBinding.envKeys
});
}
//#endregion
//#region src/gateway/node-invoke-system-run-approval.ts
function asRecord(value) {
if (!value || typeof value !== "object" || Array.isArray(value)) return null;
return value;
}
function normalizeString(value) {
if (typeof value !== "string") return null;
const trimmed = value.trim();
return trimmed ? trimmed : null;
}
function normalizeApprovalDecision(value) {
const s = normalizeString(value);
return s === "allow-once" || s === "allow-always" ? s : null;
}
function clientHasApprovals(client) {
const scopes = Array.isArray(client?.connect?.scopes) ? client?.connect?.scopes : [];
return scopes.includes("operator.admin") || scopes.includes("operator.approvals");
}
function pickSystemRunParams(raw) {
const next = {};
for (const key of [
"command",
"rawCommand",
"systemRunPlan",
"cwd",
"env",
"timeoutMs",
"needsScreenRecording",
"agentId",
"sessionKey",
"runId",
"suppressNotifyOnExit"
]) if (key in raw) next[key] = raw[key];
return next;
}
/**
* Gate `system.run` approval flags (`approved`, `approvalDecision`) behind a real
* `exec.approval.*` record. This prevents users with only `operator.write` from
* bypassing node-host approvals by injecting control fields into `node.invoke`.
*/
function sanitizeSystemRunParamsForForwarding(opts) {
const obj = asRecord(opts.rawParams);
if (!obj) return {
ok: true,
params: opts.rawParams
};
const p = obj;
const approved = p.approved === true;
const requestedDecision = normalizeApprovalDecision(p.approvalDecision);
const wantsApprovalOverride = approved || requestedDecision !== null;
const next = pickSystemRunParams(obj);
if (!wantsApprovalOverride) {
const cmdTextResolution = resolveSystemRunCommandRequest({
command: p.command,
rawCommand: p.rawCommand
});
if (!cmdTextResolution.ok) return {
ok: false,
message: cmdTextResolution.message,
details: cmdTextResolution.details
};
return {
ok: true,
params: next
};
}
const runId = normalizeString(p.runId);
if (!runId) return systemRunApprovalGuardError({
code: "MISSING_RUN_ID",
message: "approval override requires params.runId"
});
const manager = opts.execApprovalManager;
if (!manager) return systemRunApprovalGuardError({
code: "APPROVALS_UNAVAILABLE",
message: "exec approvals unavailable"
});
const snapshot = manager.getSnapshot(runId);
if (!snapshot) return systemRunApprovalGuardError({
code: "UNKNOWN_APPROVAL_ID",
message: "unknown or expired approval id",
details: { runId }
});
if ((typeof opts.nowMs === "number" ? opts.nowMs : Date.now()) > snapshot.expiresAtMs) return systemRunApprovalGuardError({
code: "APPROVAL_EXPIRED",
message: "approval expired",
details: { runId }
});
const targetNodeId = normalizeString(opts.nodeId);
if (!targetNodeId) return systemRunApprovalGuardError({
code: "MISSING_NODE_ID",
message: "node.invoke requires nodeId",
details: { runId }
});
const approvalNodeId = normalizeString(snapshot.request.nodeId);
if (!approvalNodeId) return systemRunApprovalGuardError({
code: "APPROVAL_NODE_BINDING_MISSING",
message: "approval id missing node binding",
details: { runId }
});
if (approvalNodeId !== targetNodeId) return systemRunApprovalGuardError({
code: "APPROVAL_NODE_MISMATCH",
message: "approval id not valid for this node",
details: { runId }
});
const snapshotDeviceId = snapshot.requestedByDeviceId ?? null;
const clientDeviceId = opts.client?.connect?.device?.id ?? null;
if (snapshotDeviceId) {
if (snapshotDeviceId !== clientDeviceId) return systemRunApprovalGuardError({
code: "APPROVAL_DEVICE_MISMATCH",
message: "approval id not valid for this device",
details: { runId }
});
} else if (snapshot.requestedByConnId && snapshot.requestedByConnId !== (opts.client?.connId ?? null)) return systemRunApprovalGuardError({
code: "APPROVAL_CLIENT_MISMATCH",
message: "approval id not valid for this client",
details: { runId }
});
const runtimeContext = resolveSystemRunApprovalRuntimeContext({
plan: snapshot.request.systemRunPlan ?? null,
command: p.command,
rawCommand: p.rawCommand,
cwd: p.cwd,
agentId: p.agentId,
sessionKey: p.sessionKey
});
if (!runtimeContext.ok) return {
ok: false,
message: runtimeContext.message,
details: runtimeContext.details
};
if (runtimeContext.plan) {
next.command = [...runtimeContext.plan.argv];
next.systemRunPlan = runtimeContext.plan;
if (runtimeContext.commandText) next.rawCommand = runtimeContext.commandText;
else delete next.rawCommand;
if (runtimeContext.cwd) next.cwd = runtimeContext.cwd;
else delete next.cwd;
if (runtimeContext.agentId) next.agentId = runtimeContext.agentId;
else delete next.agentId;
if (runtimeContext.sessionKey) next.sessionKey = runtimeContext.sessionKey;
else delete next.sessionKey;
}
const approvalMatch = evaluateSystemRunApprovalMatch({
argv: runtimeContext.argv,
request: snapshot.request,
binding: {
cwd: runtimeContext.cwd,
agentId: runtimeContext.agentId,
sessionKey: runtimeContext.sessionKey,
env: p.env
}
});
if (!approvalMatch.ok) return toSystemRunApprovalMismatchError({
runId,
match: approvalMatch
});
if (snapshot.decision === "allow-once") {
if (typeof manager.consumeAllowOnce !== "function" || !manager.consumeAllowOnce(runId)) return systemRunApprovalRequired(runId);
next.approved = true;
next.approvalDecision = "allow-once";
return {
ok: true,
params: next
};
}
if (snapshot.decision === "allow-always") {
next.approved = true;
next.approvalDecision = "allow-always";
return {
ok: true,
params: next
};
}
if (snapshot.resolvedAtMs !== void 0 && snapshot.decision === void 0 && snapshot.resolvedBy === null && approved && requestedDecision === "allow-once" && clientHasApprovals(opts.client)) {
next.approved = true;
next.approvalDecision = "allow-once";
return {
ok: true,
params: next
};
}
return systemRunApprovalRequired(runId);
}
//#endregion
//#region src/gateway/node-invoke-sanitize.ts
function sanitizeNodeInvokeParamsForForwarding(opts) {
if (opts.command === "system.run") return sanitizeSystemRunParamsForForwarding({
nodeId: opts.nodeId,
rawParams: opts.rawParams,
client: opts.client,
execApprovalManager: opts.execApprovalManager
});
return {
ok: true,
params: opts.rawParams
};
}
//#endregion
//#region src/gateway/server-methods/nodes.handlers.invoke-result.ts
function normalizeNodeInvokeResultParams(params) {
if (!params || typeof params !== "object") return params;
const normalized = { ...params };
if (normalized.payloadJSON === null) delete normalized.payloadJSON;
else if (normalized.payloadJSON !== void 0 && typeof normalized.payloadJSON !== "string") {
if (normalized.payload === void 0) normalized.payload = normalized.payloadJSON;
delete normalized.payloadJSON;
}
if (normalized.error === null) delete normalized.error;
return normalized;
}
const handleNodeInvokeResult = async ({ params, respond, context, client }) => {
const normalizedParams = normalizeNodeInvokeResultParams(params);
if (!validateNodeInvokeResultParams(normalizedParams)) {
respondInvalidParams({
respond,
method: "node.invoke.result",
validator: validateNodeInvokeResultParams
});
return;
}
const p = normalizedParams;
const callerNodeId = client?.connect?.device?.id ?? client?.connect?.client?.id;
if (callerNodeId && callerNodeId !== p.nodeId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId mismatch"));
return;
}
if (!context.nodeRegistry.handleInvokeResult({
id: p.id,
nodeId: p.nodeId,
ok: p.ok,
payload: p.payload,
payloadJSON: p.payloadJSON ?? null,
error: p.error ?? null
})) {
context.logGateway.debug(`late invoke result ignored: id=${p.id} node=${p.nodeId}`);
respond(true, {
ok: true,
ignored: true
}, void 0);
return;
}
respond(true, { ok: true }, void 0);
};
//#endregion
//#region src/gateway/server-methods/nodes.ts
const NODE_WAKE_RECONNECT_WAIT_MS = 3e3;
const NODE_WAKE_RECONNECT_RETRY_WAIT_MS = 12e3;
const NODE_WAKE_THROTTLE_MS = 15e3;
const NODE_WAKE_NUDGE_THROTTLE_MS = 10 * 6e4;
const NODE_PENDING_ACTION_TTL_MS = 10 * 6e4;
const NODE_PENDING_ACTION_MAX_PER_NODE = 64;
const nodeWakeById = /* @__PURE__ */ new Map();
const nodeWakeNudgeById = /* @__PURE__ */ new Map();
const pendingNodeActionsById = /* @__PURE__ */ new Map();
async function resolveDirectNodePushConfig() {
const auth = await resolveApnsAuthConfigFromEnv(process.env);
return auth.ok ? {
ok: true,
auth: auth.value
} : {
ok: false,
error: auth.error
};
}
function resolveRelayNodePushConfig() {
const relay = resolveApnsRelayConfigFromEnv(process.env, loadConfig().gateway);
return relay.ok ? {
ok: true,
relayConfig: relay.value
} : {
ok: false,
error: relay.error
};
}
async function clearStaleApnsRegistrationIfNeeded(registration, nodeId, params) {
if (!shouldClearStoredApnsRegistration({
registration,
result: params
})) return;
await clearApnsRegistrationIfCurrent({
nodeId,
registration
});
}
function isNodeEntry(entry) {
if (entry.role === "node") return true;
if (Array.isArray(entry.roles) && entry.roles.includes("node")) return true;
return false;
}
async function delayMs(ms) {
await new Promise((resolve) => setTimeout(resolve, ms));
}
function isForegroundRestrictedIosCommand(command) {
return command === "canvas.present" || command === "canvas.navigate" || command.startsWith("canvas.") || command.startsWith("camera.") || command.startsWith("screen.") || command.startsWith("talk.");
}
function shouldQueueAsPendingForegroundAction(params) {
const platform = (params.platform ?? "").trim().toLowerCase();
if (!platform.startsWith("ios") && !platform.startsWith("ipados")) return false;
if (!isForegroundRestrictedIosCommand(params.command)) return false;
const error = params.error && typeof params.error === "object" ? params.error : null;
const code = typeof error?.code === "string" ? error.code.trim().toUpperCase() : "";
const message = typeof error?.message === "string" ? error.message.trim().toUpperCase() : "";
return code === "NODE_BACKGROUND_UNAVAILABLE" || message.includes("BACKGROUND_UNAVAILABLE");
}
function prunePendingNodeActions(nodeId, nowMs) {
const queue = pendingNodeActionsById.get(nodeId) ?? [];
const minTimestampMs = nowMs - NODE_PENDING_ACTION_TTL_MS;
const live = queue.filter((entry) => entry.enqueuedAtMs >= minTimestampMs);
if (live.length === 0) {
pendingNodeActionsById.delete(nodeId);
return [];
}
pendingNodeActionsById.set(nodeId, live);
return live;
}
function enqueuePendingNodeAction(params) {
const nowMs = Date.now();
const queue = prunePendingNodeActions(params.nodeId, nowMs);
const existing = queue.find((entry) => entry.idempotencyKey === params.idempotencyKey);
if (existing) return existing;
const entry = {
id: randomUUID(),
nodeId: params.nodeId,
command: params.command,
paramsJSON: params.paramsJSON,
idempotencyKey: params.idempotencyKey,
enqueuedAtMs: nowMs
};
queue.push(entry);
if (queue.length > NODE_PENDING_ACTION_MAX_PER_NODE) queue.splice(0, queue.length - NODE_PENDING_ACTION_MAX_PER_NODE);
pendingNodeActionsById.set(params.nodeId, queue);
return entry;
}
function listPendingNodeActions(nodeId) {
return prunePendingNodeActions(nodeId, Date.now());
}
function resolveAllowedPendingNodeActions(params) {
const pending = listPendingNodeActions(params.nodeId);
if (pending.length === 0) return pending;
const connect = params.client?.connect;
const declaredCommands = Array.isArray(connect?.commands) ? connect.commands : [];
const allowlist = resolveNodeCommandAllowlist(loadConfig(), {
platform: connect?.client?.platform,
deviceFamily: connect?.client?.deviceFamily
});
const allowed = pending.filter((entry) => {
return isNodeCommandAllowed({
command: entry.command,
declaredCommands,
allowlist
}).ok;
});
if (allowed.length !== pending.length) if (allowed.length === 0) pendingNodeActionsById.delete(params.nodeId);
else pendingNodeActionsById.set(params.nodeId, allowed);
return allowed;
}
function ackPendingNodeActions(nodeId, ids) {
if (ids.length === 0) return listPendingNodeActions(nodeId);
const pending = prunePendingNodeActions(nodeId, Date.now());
const idSet = new Set(ids);
const remaining = pending.filter((entry) => !idSet.has(entry.id));
if (remaining.length === 0) {
pendingNodeActionsById.delete(nodeId);
return [];
}
pendingNodeActionsById.set(nodeId, remaining);
return remaining;
}
function toPendingParamsJSON(params) {
if (params === void 0) return;
try {
return JSON.stringify(params);
} catch {
return;
}
}
async function maybeWakeNodeWithApns(nodeId, opts) {
const state = nodeWakeById.get(nodeId) ?? { lastWakeAtMs: 0 };
nodeWakeById.set(nodeId, state);
if (state.inFlight) return await state.inFlight;
const now = Date.now();
if (!(opts?.force === true) && state.lastWakeAtMs > 0 && now - state.lastWakeAtMs < NODE_WAKE_THROTTLE_MS) return {
available: true,
throttled: true,
path: "throttled",
durationMs: 0
};
state.inFlight = (async () => {
const startedAtMs = Date.now();
const withDuration = (attempt) => ({
...attempt,
durationMs: Math.max(0, Date.now() - startedAtMs)
});
try {
const registration = await loadApnsRegistration(nodeId);
if (!registration) return withDuration({
available: false,
throttled: false,
path: "no-registration"
});
let wakeResult;
if (registration.transport === "relay") {
const relay = resolveRelayNodePushConfig();
if (!relay.ok) return withDuration({
available: false,
throttled: false,
path: "no-auth",
apnsReason: relay.error
});
state.lastWakeAtMs = Date.now();
wakeResult = await sendApnsBackgroundWake({
registration,
nodeId,
wakeReason: opts?.wakeReason ?? "node.invoke",
relayConfig: relay.relayConfig
});
} else {
const auth = await resolveDirectNodePushConfig();
if (!auth.ok) return withDuration({
available: false,
throttled: false,
path: "no-auth",
apnsReason: auth.error
});
state.lastWakeAtMs = Date.now();
wakeResult = await sendApnsBackgroundWake({
registration,
nodeId,
wakeReason: opts?.wakeReason ?? "node.invoke",
auth: auth.auth
});
}
await clearStaleApnsRegistrationIfNeeded(registration, nodeId, wakeResult);
if (!wakeResult.ok) return withDuration({
available: true,
throttled: false,
path: "send-error",
apnsStatus: wakeResult.status,
apnsReason: wakeResult.reason
});
return withDuration({
available: true,
throttled: false,
path: "sent",
apnsStatus: wakeResult.status,
apnsReason: wakeResult.reason
});
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
if (state.lastWakeAtMs === 0) return withDuration({
available: false,
throttled: false,
path: "send-error",
apnsReason: message
});
return withDuration({
available: true,
throttled: false,
path: "send-error",
apnsReason: message
});
}
})();
try {
return await state.inFlight;
} finally {
state.inFlight = void 0;
}
}
async function maybeSendNodeWakeNudge(nodeId) {
const startedAtMs = Date.now();
const withDuration = (attempt) => ({
...attempt,
durationMs: Math.max(0, Date.now() - startedAtMs)
});
const lastNudgeAtMs = nodeWakeNudgeById.get(nodeId) ?? 0;
if (lastNudgeAtMs > 0 && Date.now() - lastNudgeAtMs < NODE_WAKE_NUDGE_THROTTLE_MS) return withDuration({
sent: false,
throttled: true,
reason: "throttled"
});
const registration = await loadApnsRegistration(nodeId);
if (!registration) return withDuration({
sent: false,
throttled: false,
reason: "no-registration"
});
try {
let result;
if (registration.transport === "relay") {
const relay = resolveRelayNodePushConfig();
if (!relay.ok) return withDuration({
sent: false,
throttled: false,
reason: "no-auth",
apnsReason: relay.error
});
result = await sendApnsAlert({
registration,
nodeId,
title: "OpenClaw needs a quick reopen",
body: "Tap to reopen OpenClaw and restore the node connection.",
relayConfig: relay.relayConfig
});
} else {
const auth = await resolveDirectNodePushConfig();
if (!auth.ok) return withDuration({
sent: false,
throttled: false,
reason: "no-auth",
apnsReason: auth.error
});
result = await sendApnsAlert({
registration,
nodeId,
title: "OpenClaw needs a quick reopen",
body: "Tap to reopen OpenClaw and restore the node connection.",
auth: auth.auth
});
}
await clearStaleApnsRegistrationIfNeeded(registration, nodeId, result);
if (!result.ok) return withDuration({
sent: false,
throttled: false,
reason: "apns-not-ok",
apnsStatus: result.status,
apnsReason: result.reason
});
nodeWakeNudgeById.set(nodeId, Date.now());
return withDuration({
sent: true,
throttled: false,
reason: "sent",
apnsStatus: result.status,
apnsReason: result.reason
});
} catch (err) {
return withDuration({
sent: false,
throttled: false,
reason: "send-error",
apnsReason: err instanceof Error ? err.message : String(err)
});
}
}
async function waitForNodeReconnect(params) {
const timeoutMs = Math.max(250, params.timeoutMs ?? 3e3);
const pollMs = Math.max(50, params.pollMs ?? 150);
const deadline = Date.now() + timeoutMs;
while (Date.now() < deadline) {
if (params.context.nodeRegistry.get(params.nodeId)) return true;
await delayMs(pollMs);
}
return Boolean(params.context.nodeRegistry.get(params.nodeId));
}
const nodeHandlers = {
"node.pair.request": async ({ params, respond, context }) => {
if (!validateNodePairRequestParams(params)) {
respondInvalidParams({
respond,
method: "node.pair.request",
validator: validateNodePairRequestParams
});
return;
}
const p = params;
await respondUnavailableOnThrow(respond, async () => {
const result = await requestNodePairing({
nodeId: p.nodeId,
displayName: p.displayName,
platform: p.platform,
version: p.version,
coreVersion: p.coreVersion,
uiVersion: p.uiVersion,
deviceFamily: p.deviceFamily,
modelIdentifier: p.modelIdentifier,
caps: p.caps,
commands: p.commands,
permissions: p.permissions,
remoteIp: p.remoteIp,
silent: p.silent
});
if (result.status === "pending" && result.created) context.broadcast("node.pair.requested", result.request, { dropIfSlow: true });
respond(true, result, void 0);
});
},
"node.pair.list": async ({ params, respond }) => {
if (!validateNodePairListParams(params)) {
respondInvalidParams({
respond,
method: "node.pair.list",
validator: validateNodePairListParams
});
return;
}
await respondUnavailableOnThrow(respond, async () => {
respond(true, await listNodePairing(), void 0);
});
},
"node.pair.approve": async ({ params, respond, context }) => {
if (!validateNodePairApproveParams(params)) {
respondInvalidParams({
respond,
method: "node.pair.approve",
validator: validateNodePairApproveParams
});
return;
}
const { requestId } = params;
await respondUnavailableOnThrow(respond, async () => {
const approved = await approveNodePairing(requestId);
if (!approved) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown requestId"));
return;
}
context.broadcast("node.pair.resolved", {
requestId,
nodeId: approved.node.nodeId,
decision: "approved",
ts: Date.now()
}, { dropIfSlow: true });
respond(true, approved, void 0);
});
},
"node.pair.reject": async ({ params, respond, context }) => {
if (!validateNodePairRejectParams(params)) {
respondInvalidParams({
respond,
method: "node.pair.reject",
validator: validateNodePairRejectParams
});
return;
}
const { requestId } = params;
await respondUnavailableOnThrow(respond, async () => {
const rejected = await rejectNodePairing(requestId);
if (!rejected) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown requestId"));
return;
}
context.broadcast("node.pair.resolved", {
requestId,
nodeId: rejected.nodeId,
decision: "rejected",
ts: Date.now()
}, { dropIfSlow: true });
respond(true, rejected, void 0);
});
},
"node.pair.verify": async ({ params, respond }) => {
if (!validateNodePairVerifyParams(params)) {
respondInvalidParams({
respond,
method: "node.pair.verify",
validator: validateNodePairVerifyParams
});
return;
}
const { nodeId, token } = params;
await respondUnavailableOnThrow(respond, async () => {
respond(true, await verifyNodeToken(nodeId, token), void 0);
});
},
"node.rename": async ({ params, respond }) => {
if (!validateNodeRenameParams(params)) {
respondInvalidParams({
respond,
method: "node.rename",
validator: validateNodeRenameParams
});
return;
}
const { nodeId, displayName } = params;
await respondUnavailableOnThrow(respond, async () => {
const trimmed = displayName.trim();
if (!trimmed) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "displayName required"));
return;
}
const updated = await renamePairedNode(nodeId, trimmed);
if (!updated) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown nodeId"));
return;
}
respond(true, {
nodeId: updated.nodeId,
displayName: updated.displayName
}, void 0);
});
},
"node.list": async ({ params, respond, context }) => {
if (!validateNodeListParams(params)) {
respondInvalidParams({
respond,
method: "node.list",
validator: validateNodeListParams
});
return;
}
await respondUnavailableOnThrow(respond, async () => {
const list = await listDevicePairing();
const pairedById = new Map(list.paired.filter((entry) => isNodeEntry(entry)).map((entry) => [entry.deviceId, {
nodeId: entry.deviceId,
displayName: entry.displayName,
platform: entry.platform,
version: void 0,
coreVersion: void 0,
uiVersion: void 0,
deviceFamily: void 0,
modelIdentifier: void 0,
remoteIp: entry.remoteIp,
caps: [],
commands: [],
permissions: void 0
}]));
const connected = context.nodeRegistry.listConnected();
const connectedById = new Map(connected.map((n) => [n.nodeId, n]));
const nodes = [...new Set([...pairedById.keys(), ...connectedById.keys()])].map((nodeId) => {
const paired = pairedById.get(nodeId);
const live = connectedById.get(nodeId);
const caps = uniqueSortedStrings([...live?.caps ?? paired?.caps ?? []]);
const commands = uniqueSortedStrings([...live?.commands ?? paired?.commands ?? []]);
return {
nodeId,
displayName: live?.displayName ?? paired?.displayName,
platform: live?.platform ?? paired?.platform,
version: live?.version ?? paired?.version,
coreVersion: live?.coreVersion ?? paired?.coreVersion,
uiVersion: live?.uiVersion ?? paired?.uiVersion,
deviceFamily: live?.deviceFamily ?? paired?.deviceFamily,
modelIdentifier: live?.modelIdentifier ?? paired?.modelIdentifier,
remoteIp: live?.remoteIp ?? paired?.remoteIp,
caps,
commands,
pathEnv: live?.pathEnv,
permissions: live?.permissions ?? paired?.permissions,
connectedAtMs: live?.connectedAtMs,
paired: Boolean(paired),
connected: Boolean(live)
};
});
nodes.sort((a, b) => {
if (a.connected !== b.connected) return a.connected ? -1 : 1;
const an = (a.displayName ?? a.nodeId).toLowerCase();
const bn = (b.displayName ?? b.nodeId).toLowerCase();
if (an < bn) return -1;
if (an > bn) return 1;
return a.nodeId.localeCompare(b.nodeId);
});
respond(true, {
ts: Date.now(),
nodes
}, void 0);
});
},
"node.describe": async ({ params, respond, context }) => {
if (!validateNodeDescribeParams(params)) {
respondInvalidParams({
respond,
method: "node.describe",
validator: validateNodeDescribeParams
});
return;
}
const { nodeId } = params;
const id = String(nodeId ?? "").trim();
if (!id) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId required"));
return;
}
await respondUnavailableOnThrow(respond, async () => {
const paired = (await listDevicePairing()).paired.find((n) => n.deviceId === id && isNodeEntry(n));
const live = context.nodeRegistry.listConnected().find((n) => n.nodeId === id);
if (!paired && !live) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown nodeId"));
return;
}
const caps = uniqueSortedStrings([...live?.caps ?? []]);
const commands = uniqueSortedStrings([...live?.commands ?? []]);
respond(true, {
ts: Date.now(),
nodeId: id,
displayName: live?.displayName ?? paired?.displayName,
platform: live?.platform ?? paired?.platform,
version: live?.version,
coreVersion: live?.coreVersion,
uiVersion: live?.uiVersion,
deviceFamily: live?.deviceFamily,
modelIdentifier: live?.modelIdentifier,
remoteIp: live?.remoteIp ?? paired?.remoteIp,
caps,
commands,
pathEnv: live?.pathEnv,
permissions: live?.permissions,
connectedAtMs: live?.connectedAtMs,
paired: Boolean(paired),
connected: Boolean(live)
}, void 0);
});
},
"node.canvas.capability.refresh": async ({ params, respond, client }) => {
if (!validateNodeListParams(params)) {
respondInvalidParams({
respond,
method: "node.canvas.capability.refresh",
validator: validateNodeListParams
});
return;
}
const baseCanvasHostUrl = client?.canvasHostUrl?.trim() ?? "";
if (!baseCanvasHostUrl) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "canvas host unavailable for this node session"));
return;
}
const canvasCapability = mintCanvasCapabilityToken();
const canvasCapabilityExpiresAtMs = Date.now() + CANVAS_CAPABILITY_TTL_MS;
const scopedCanvasHostUrl = buildCanvasScopedHostUrl(baseCanvasHostUrl, canvasCapability);
if (!scopedCanvasHostUrl) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "failed to mint scoped canvas host URL"));
return;
}
if (client) {
client.canvasCapability = canvasCapability;
client.canvasCapabilityExpiresAtMs = canvasCapabilityExpiresAtMs;
}
respond(true, {
canvasCapability,
canvasCapabilityExpiresAtMs,
canvasHostUrl: scopedCanvasHostUrl
}, void 0);
},
"node.pending.pull": async ({ params, respond, client }) => {
if (!validateNodeListParams(params)) {
respondInvalidParams({
respond,
method: "node.pending.pull",
validator: validateNodeListParams
});
return;
}
const nodeId = client?.connect?.device?.id ?? client?.connect?.client?.id;
const trimmedNodeId = String(nodeId ?? "").trim();
if (!trimmedNodeId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId required"));
return;
}
respond(true, {
nodeId: trimmedNodeId,
actions: resolveAllowedPendingNodeActions({
nodeId: trimmedNodeId,
client
}).map((entry) => ({
id: entry.id,
command: entry.command,
paramsJSON: entry.paramsJSON ?? null,
enqueuedAtMs: entry.enqueuedAtMs
}))
}, void 0);
},
"node.pending.ack": async ({ params, respond, client }) => {
if (!validateNodePendingAckParams(params)) {
respondInvalidParams({
respond,
method: "node.pending.ack",
validator: validateNodePendingAckParams
});
return;
}
const nodeId = client?.connect?.device?.id ?? client?.connect?.client?.id;
const trimmedNodeId = String(nodeId ?? "").trim();
if (!trimmedNodeId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId required"));
return;
}
const ackIds = Array.from(new Set((params.ids ?? []).map((value) => String(value ?? "").trim()).filter(Boolean)));
respond(true, {
nodeId: trimmedNodeId,
ackedIds: ackIds,
remainingCount: ackPendingNodeActions(trimmedNodeId, ackIds).length
}, void 0);
},
"node.invoke": async ({ params, respond, context, client, req }) => {
if (!validateNodeInvokeParams(params)) {
respondInvalidParams({
respond,
method: "node.invoke",
validator: validateNodeInvokeParams
});
return;
}
const p = params;
const nodeId = String(p.nodeId ?? "").trim();
const command = String(p.command ?? "").trim();
if (!nodeId || !command) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId and command required"));
return;
}
if (command === "system.execApprovals.get" || command === "system.execApprovals.set") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "node.invoke does not allow system.execApprovals.*; use exec.approvals.node.*", { details: { command } }));
return;
}
await respondUnavailableOnThrow(respond, async () => {
let nodeSession = context.nodeRegistry.get(nodeId);
if (!nodeSession) {
const wakeReqId = req.id;
const wakeFlowStartedAtMs = Date.now();
context.logGateway.info(`node wake start node=${nodeId} req=${wakeReqId} command=${command}`);
const wake = await maybeWakeNodeWithApns(nodeId);
context.logGateway.info(`node wake stage=wake1 node=${nodeId} req=${wakeReqId} available=${wake.available} throttled=${wake.throttled} path=${wake.path} durationMs=${wake.durationMs} apnsStatus=${wake.apnsStatus ?? -1} apnsReason=${wake.apnsReason ?? "-"}`);
if (wake.available) {
const waitStartedAtMs = Date.now();
const waitTimeoutMs = NODE_WAKE_RECONNECT_WAIT_MS;
const reconnected = await waitForNodeReconnect({
nodeId,
context,
timeoutMs: waitTimeoutMs
});
const waitDurationMs = Math.max(0, Date.now() - waitStartedAtMs);
context.logGateway.info(`node wake stage=wait1 node=${nodeId} req=${wakeReqId} reconnected=${reconnected} timeoutMs=${waitTimeoutMs} durationMs=${waitDurationMs}`);
}
nodeSession = context.nodeRegistry.get(nodeId);
if (!nodeSession && wake.available) {
const retryWake = await maybeWakeNodeWithApns(nodeId, { force: true });
context.logGateway.info(`node wake stage=wake2 node=${nodeId} req=${wakeReqId} force=true available=${retryWake.available} throttled=${retryWake.throttled} path=${retryWake.path} durationMs=${retryWake.durationMs} apnsStatus=${retryWake.apnsStatus ?? -1} apnsReason=${retryWake.apnsReason ?? "-"}`);
if (retryWake.available) {
const waitStartedAtMs = Date.now();
const waitTimeoutMs = NODE_WAKE_RECONNECT_RETRY_WAIT_MS;
const reconnected = await waitForNodeReconnect({
nodeId,
context,
timeoutMs: waitTimeoutMs
});
const waitDurationMs = Math.max(0, Date.now() - waitStartedAtMs);
context.logGateway.info(`node wake stage=wait2 node=${nodeId} req=${wakeReqId} reconnected=${reconnected} timeoutMs=${waitTimeoutMs} durationMs=${waitDurationMs}`);
}
nodeSession = context.nodeRegistry.get(nodeId);
}
if (!nodeSession) {
const totalDurationMs = Math.max(0, Date.now() - wakeFlowStartedAtMs);
const nudge = await maybeSendNodeWakeNudge(nodeId);
context.logGateway.info(`node wake nudge node=${nodeId} req=${wakeReqId} sent=${nudge.sent} throttled=${nudge.throttled} reason=${nudge.reason} durationMs=${nudge.durationMs} apnsStatus=${nudge.apnsStatus ?? -1} apnsReason=${nudge.apnsReason ?? "-"}`);
context.logGateway.warn(`node wake done node=${nodeId} req=${wakeReqId} connected=false reason=not_connected totalMs=${totalDurationMs}`);
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "node not connected", { details: { code: "NOT_CONNECTED" } }));
return;
}
const totalDurationMs = Math.max(0, Date.now() - wakeFlowStartedAtMs);
context.logGateway.info(`node wake done node=${nodeId} req=${wakeReqId} connected=true totalMs=${totalDurationMs}`);
}
const allowlist = resolveNodeCommandAllowlist(loadConfig(), nodeSession);
const allowed = isNodeCommandAllowed({
command,
declaredCommands: nodeSession.commands,
allowlist
});
if (!allowed.ok) {
const hint = buildNodeCommandRejectionHint(allowed.reason, command, nodeSession);
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, hint, { details: {
reason: allowed.reason,
command
} }));
return;
}
const forwardedParams = sanitizeNodeInvokeParamsForForwarding({
nodeId,
command,
rawParams: p.params,
client,
execApprovalManager: context.execApprovalManager
});
if (!forwardedParams.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, forwardedParams.message, { details: forwardedParams.details ?? null }));
return;
}
const res = await context.nodeRegistry.invoke({
nodeId,
command,
params: forwardedParams.params,
timeoutMs: p.timeoutMs,
idempotencyKey: p.idempotencyKey
});
if (!res.ok) {
if (shouldQueueAsPendingForegroundAction({
platform: nodeSession.platform,
command,
error: res.error
})) {
const queued = enqueuePendingNodeAction({
nodeId,
command,
paramsJSON: toPendingParamsJSON(forwardedParams.params),
idempotencyKey: p.idempotencyKey
});
const wake = await maybeWakeNodeWithApns(nodeId);
context.logGateway.info(`node pending queued node=${nodeId} req=${req.id} command=${command} queuedId=${queued.id} wakePath=${wake.path} wakeAvailable=${wake.available}`);
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "node command queued until iOS returns to foreground", {
retryable: true,
details: {
code: "QUEUED_UNTIL_FOREGROUND",
queuedActionId: queued.id,
nodeId,
command,
wake: {
path: wake.path,
available: wake.available,
throttled: wake.throttled,
apnsStatus: wake.apnsStatus,
apnsReason: wake.apnsReason
},
nodeError: res.error ?? null
}
}));
return;
}
if (!respondUnavailableOnNodeInvokeError(respond, res)) return;
return;
}
respond(true, {
ok: true,
nodeId,
command,
payload: res.payloadJSON ? safeParseJson(res.payloadJSON) : res.payload,
payloadJSON: res.payloadJSON ?? null
}, void 0);
});
},
"node.invoke.result": handleNodeInvokeResult,
"node.event": async ({ params, respond, context, client }) => {
if (!validateNodeEventParams(params)) {
respondInvalidParams({
respond,
method: "node.event",
validator: validateNodeEventParams
});
return;
}
const p = params;
const payloadJSON = typeof p.payloadJSON === "string" ? p.payloadJSON : p.payload !== void 0 ? JSON.stringify(p.payload) : null;
await respondUnavailableOnThrow(respond, async () => {
const { handleNodeEvent } = await import("./server-node-events-sWsLB66P.js");
const nodeId = client?.connect?.device?.id ?? client?.connect?.client?.id ?? "node";
await handleNodeEvent({
deps: context.deps,
broadcast: context.broadcast,
nodeSendToSession: context.nodeSendToSession,
nodeSubscribe: context.nodeSubscribe,
nodeUnsubscribe: context.nodeUnsubscribe,
broadcastVoiceWakeChanged: context.broadcastVoiceWakeChanged,
addChatRun: context.addChatRun,
removeChatRun: context.removeChatRun,
chatAbortControllers: context.chatAbortControllers,
chatAbortedRuns: context.chatAbortedRuns,
chatRunBuffers: context.chatRunBuffers,
chatDeltaSentAt: context.chatDeltaSentAt,
dedupe: context.dedupe,
agentRunSeq: context.agentRunSeq,
getHealthCache: context.getHealthCache,
refreshHealthSnapshot: context.refreshHealthSnapshot,
loadGatewayModelCatalog: context.loadGatewayModelCatalog,
logGateway: { warn: context.logGateway.warn }
}, nodeId, {
event: p.event,
payloadJSON
});
respond(true, { ok: true }, void 0);
});
}
};
function buildNodeCommandRejectionHint(reason, command, node) {
const platform = node?.platform ?? "unknown";
if (reason === "command not declared by node") return `node command not allowed: the node (platform: ${platform}) does not support "${command}"`;
if (reason === "command not allowlisted") return `node command not allowed: "${command}" is not in the allowlist for platform "${platform}"`;
if (reason === "node did not declare commands") return `node command not allowed: the node did not declare any supported commands`;
return `node command not allowed: ${reason}`;
}
//#endregion
//#region src/gateway/server-methods/nodes-pending.ts
function resolveClientNodeId(client) {
const trimmed = (client?.connect?.device?.id ?? client?.connect?.client?.id ?? "").trim();
return trimmed.length > 0 ? trimmed : null;
}
const nodePendingHandlers = {
"node.pending.drain": async ({ params, respond, client }) => {
if (!validateNodePendingDrainParams(params)) {
respondInvalidParams({
respond,
method: "node.pending.drain",
validator: validateNodePendingDrainParams
});
return;
}
const nodeId = resolveClientNodeId(client);
if (!nodeId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "node.pending.drain requires a connected device identity"));
return;
}
respond(true, {
nodeId,
...drainNodePendingWork(nodeId, {
maxItems: params.maxItems,
includeDefaultStatus: true
})
}, void 0);
},
"node.pending.enqueue": async ({ params, respond, context }) => {
if (!validateNodePendingEnqueueParams(params)) {
respondInvalidParams({
respond,
method: "node.pending.enqueue",
validator: validateNodePendingEnqueueParams
});
return;
}
const p = params;
await respondUnavailableOnThrow(respond, async () => {
const queued = enqueueNodePendingWork({
nodeId: p.nodeId,
type: p.type,
priority: p.priority,
expiresInMs: p.expiresInMs
});
let wakeTriggered = false;
if (p.wake !== false && !queued.deduped && !context.nodeRegistry.get(p.nodeId)) {
const wakeReqId = queued.item.id;
context.logGateway.info(`node pending wake start node=${p.nodeId} req=${wakeReqId} type=${queued.item.type}`);
const wake = await maybeWakeNodeWithApns(p.nodeId, { wakeReason: "node.pending" });
context.logGateway.info(`node pending wake stage=wake1 node=${p.nodeId} req=${wakeReqId} available=${wake.available} throttled=${wake.throttled} path=${wake.path} durationMs=${wake.durationMs} apnsStatus=${wake.apnsStatus ?? -1} apnsReason=${wake.apnsReason ?? "-"}`);
wakeTriggered = wake.available;
if (wake.available) {
const reconnected = await waitForNodeReconnect({
nodeId: p.nodeId,
context,
timeoutMs: NODE_WAKE_RECONNECT_WAIT_MS
});
context.logGateway.info(`node pending wake stage=wait1 node=${p.nodeId} req=${wakeReqId} reconnected=${reconnected} timeoutMs=${NODE_WAKE_RECONNECT_WAIT_MS}`);
}
if (!context.nodeRegistry.get(p.nodeId) && wake.available) {
const retryWake = await maybeWakeNodeWithApns(p.nodeId, {
force: true,
wakeReason: "node.pending"
});
context.logGateway.info(`node pending wake stage=wake2 node=${p.nodeId} req=${wakeReqId} force=true available=${retryWake.available} throttled=${retryWake.throttled} path=${retryWake.path} durationMs=${retryWake.durationMs} apnsStatus=${retryWake.apnsStatus ?? -1} apnsReason=${retryWake.apnsReason ?? "-"}`);
if (retryWake.available) {
const reconnected = await waitForNodeReconnect({
nodeId: p.nodeId,
context,
timeoutMs: NODE_WAKE_RECONNECT_RETRY_WAIT_MS
});
context.logGateway.info(`node pending wake stage=wait2 node=${p.nodeId} req=${wakeReqId} reconnected=${reconnected} timeoutMs=${NODE_WAKE_RECONNECT_RETRY_WAIT_MS}`);
}
}
if (!context.nodeRegistry.get(p.nodeId)) {
const nudge = await maybeSendNodeWakeNudge(p.nodeId);
context.logGateway.info(`node pending wake nudge node=${p.nodeId} req=${wakeReqId} sent=${nudge.sent} throttled=${nudge.throttled} reason=${nudge.reason} durationMs=${nudge.durationMs} apnsStatus=${nudge.apnsStatus ?? -1} apnsReason=${nudge.apnsReason ?? "-"}`);
context.logGateway.warn(`node pending wake done node=${p.nodeId} req=${wakeReqId} connected=false reason=not_connected`);
} else context.logGateway.info(`node pending wake done node=${p.nodeId} req=${wakeReqId} connected=true`);
}
respond(true, {
nodeId: p.nodeId,
revision: queued.revision,
queued: queued.item,
wakeTriggered
}, void 0);
});
}
};
//#endregion
//#region src/gateway/server-methods/push.ts
function normalizeOptionalString(value) {
if (typeof value !== "string") return;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : void 0;
}
const pushHandlers = { "push.test": async ({ params, respond }) => {
if (!validatePushTestParams(params)) {
respondInvalidParams({
respond,
method: "push.test",
validator: validatePushTestParams
});
return;
}
const nodeId = String(params.nodeId ?? "").trim();
if (!nodeId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId required"));
return;
}
const title = normalizeOptionalString(params.title) ?? "OpenClaw";
const body = normalizeOptionalString(params.body) ?? `Push test for node ${nodeId}`;
await respondUnavailableOnThrow(respond, async () => {
const registration = await loadApnsRegistration(nodeId);
if (!registration) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `node ${nodeId} has no APNs registration (connect iOS node first)`));
return;
}
const overrideEnvironment = normalizeApnsEnvironment(params.environment);
const result = registration.transport === "direct" ? await (async () => {
const auth = await resolveApnsAuthConfigFromEnv(process.env);
if (!auth.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, auth.error));
return null;
}
return await sendApnsAlert({
registration: {
...registration,
environment: overrideEnvironment ?? registration.environment
},
nodeId,
title,
body,
auth: auth.value
});
})() : await (async () => {
const relay = resolveApnsRelayConfigFromEnv(process.env, loadConfig().gateway);
if (!relay.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, relay.error));
return null;
}
return await sendApnsAlert({
registration,
nodeId,
title,
body,
relayConfig: relay.value
});
})();
if (!result) return;
if (shouldClearStoredApnsRegistration({
registration,
result,
overrideEnvironment
})) await clearApnsRegistrationIfCurrent({
nodeId,
registration
});
respond(true, result, void 0);
});
} };
//#endregion
//#region src/gateway/server-methods/send.ts
const inflightByContext = /* @__PURE__ */ new WeakMap();
const getInflightMap = (context) => {
let inflight = inflightByContext.get(context);
if (!inflight) {
inflight = /* @__PURE__ */ new Map();
inflightByContext.set(context, inflight);
}
return inflight;
};
async function resolveRequestedChannel(params) {
const channelInput = typeof params.requestChannel === "string" ? params.requestChannel : void 0;
const normalizedChannel = channelInput ? normalizeChannelId(channelInput) : null;
if (channelInput && !normalizedChannel) {
const normalizedInput = channelInput.trim().toLowerCase();
if (params.rejectWebchatAsInternalOnly && normalizedInput === "webchat") return { error: errorShape(ErrorCodes.INVALID_REQUEST, "unsupported channel: webchat (internal-only). Use `chat.send` for WebChat UI messages or choose a deliverable channel.") };
return { error: errorShape(ErrorCodes.INVALID_REQUEST, params.unsupportedMessage(channelInput)) };
}
const cfg = loadConfig();
let channel = normalizedChannel;
if (!channel) try {
channel = (await resolveMessageChannelSelection({ cfg })).channel;
} catch (err) {
return { error: errorShape(ErrorCodes.INVALID_REQUEST, String(err)) };
}
return {
cfg,
channel
};
}
const sendHandlers = {
send: async ({ params, respond, context }) => {
const p = params;
if (!validateSendParams(p)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid send params: ${formatValidationErrors(validateSendParams.errors)}`));
return;
}
const request = p;
const idem = request.idempotencyKey;
const dedupeKey = `send:${idem}`;
const cached = context.dedupe.get(dedupeKey);
if (cached) {
respond(cached.ok, cached.payload, cached.error, { cached: true });
return;
}
const inflightMap = getInflightMap(context);
const inflight = inflightMap.get(dedupeKey);
if (inflight) {
const result = await inflight;
const meta = result.meta ? {
...result.meta,
cached: true
} : { cached: true };
respond(result.ok, result.payload, result.error, meta);
return;
}
const to = request.to.trim();
const message = typeof request.message === "string" ? request.message.trim() : "";
const mediaUrl = typeof request.mediaUrl === "string" && request.mediaUrl.trim().length > 0 ? request.mediaUrl.trim() : void 0;
const mediaUrls = Array.isArray(request.mediaUrls) ? request.mediaUrls.map((entry) => typeof entry === "string" ? entry.trim() : "").filter((entry) => entry.length > 0) : void 0;
if (!message && !mediaUrl && (mediaUrls?.length ?? 0) === 0) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid send params: text or media is required"));
return;
}
const resolvedChannel = await resolveRequestedChannel({
requestChannel: request.channel,
unsupportedMessage: (input) => `unsupported channel: ${input}`,
rejectWebchatAsInternalOnly: true
});
if ("error" in resolvedChannel) {
respond(false, void 0, resolvedChannel.error);
return;
}
const { cfg, channel } = resolvedChannel;
const accountId = typeof request.accountId === "string" && request.accountId.trim().length ? request.accountId.trim() : void 0;
const threadId = typeof request.threadId === "string" && request.threadId.trim().length ? request.threadId.trim() : void 0;
const outboundChannel = channel;
if (!resolveOutboundChannelPlugin({
channel,
cfg
})) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unsupported channel: ${channel}`));
return;
}
const work = (async () => {
try {
const resolved = resolveOutboundTarget({
channel: outboundChannel,
to,
cfg,
accountId,
mode: "explicit"
});
if (!resolved.ok) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, String(resolved.error)),
meta: { channel }
};
const idLikeTarget = await maybeResolveIdLikeTarget({
cfg,
channel,
input: resolved.to,
accountId
});
const deliveryTarget = idLikeTarget?.to ?? resolved.to;
const outboundDeps = context.deps ? createOutboundSendDeps$1(context.deps) : void 0;
const mirrorPayloads = normalizeReplyPayloadsForDelivery([{
text: message,
mediaUrl,
mediaUrls
}]);
const mirrorText = mirrorPayloads.map((payload) => payload.text).filter(Boolean).join("\n");
const mirrorMediaUrls = mirrorPayloads.flatMap((payload) => resolveSendableOutboundReplyParts(payload).mediaUrls);
const providedSessionKey = typeof request.sessionKey === "string" && request.sessionKey.trim() ? request.sessionKey.trim().toLowerCase() : void 0;
const explicitAgentId = typeof request.agentId === "string" && request.agentId.trim() ? request.agentId.trim() : void 0;
const sessionAgentId = providedSessionKey ? resolveSessionAgentId({
sessionKey: providedSessionKey,
config: cfg
}) : void 0;
const defaultAgentId = resolveSessionAgentId({ config: cfg });
const effectiveAgentId = explicitAgentId ?? sessionAgentId ?? defaultAgentId;
const derivedRoute = !providedSessionKey ? await resolveOutboundSessionRoute({
cfg,
channel,
agentId: effectiveAgentId,
accountId,
target: deliveryTarget,
resolvedTarget: idLikeTarget,
threadId
}) : null;
if (derivedRoute) await ensureOutboundSessionEntry({
cfg,
agentId: effectiveAgentId,
channel,
accountId,
route: derivedRoute
});
const outboundSession = buildOutboundSessionContext({
cfg,
agentId: effectiveAgentId,
sessionKey: providedSessionKey ?? derivedRoute?.sessionKey
});
const result = (await deliverOutboundPayloads({
cfg,
channel: outboundChannel,
to: deliveryTarget,
accountId,
payloads: [{
text: message,
mediaUrl,
mediaUrls
}],
session: outboundSession,
gifPlayback: request.gifPlayback,
threadId: threadId ?? null,
deps: outboundDeps,
mirror: providedSessionKey ? {
sessionKey: providedSessionKey,
agentId: effectiveAgentId,
text: mirrorText || message,
mediaUrls: mirrorMediaUrls.length > 0 ? mirrorMediaUrls : void 0,
idempotencyKey: idem
} : derivedRoute ? {
sessionKey: derivedRoute.sessionKey,
agentId: effectiveAgentId,
text: mirrorText || message,
mediaUrls: mirrorMediaUrls.length > 0 ? mirrorMediaUrls : void 0,
idempotencyKey: idem
} : void 0
})).at(-1);
if (!result) throw new Error("No delivery result");
const payload = {
runId: idem,
messageId: result.messageId,
channel
};
if ("chatId" in result) payload.chatId = result.chatId;
if ("channelId" in result) payload.channelId = result.channelId;
if ("toJid" in result) payload.toJid = result.toJid;
if ("conversationId" in result) payload.conversationId = result.conversationId;
context.dedupe.set(dedupeKey, {
ts: Date.now(),
ok: true,
payload
});
return {
ok: true,
payload,
meta: { channel }
};
} catch (err) {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
context.dedupe.set(dedupeKey, {
ts: Date.now(),
ok: false,
error
});
return {
ok: false,
error,
meta: {
channel,
error: formatForLog(err)
}
};
}
})();
inflightMap.set(dedupeKey, work);
try {
const result = await work;
respond(result.ok, result.payload, result.error, result.meta);
} finally {
inflightMap.delete(dedupeKey);
}
},
poll: async ({ params, respond, context }) => {
const p = params;
if (!validatePollParams(p)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid poll params: ${formatValidationErrors(validatePollParams.errors)}`));
return;
}
const request = p;
const idem = request.idempotencyKey;
const cached = context.dedupe.get(`poll:${idem}`);
if (cached) {
respond(cached.ok, cached.payload, cached.error, { cached: true });
return;
}
const to = request.to.trim();
const resolvedChannel = await resolveRequestedChannel({
requestChannel: request.channel,
unsupportedMessage: (input) => `unsupported poll channel: ${input}`
});
if ("error" in resolvedChannel) {
respond(false, void 0, resolvedChannel.error);
return;
}
const { cfg, channel } = resolvedChannel;
if (typeof request.durationSeconds === "number" && channel !== "telegram") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "durationSeconds is only supported for Telegram polls"));
return;
}
if (typeof request.isAnonymous === "boolean" && channel !== "telegram") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "isAnonymous is only supported for Telegram polls"));
return;
}
const poll = {
question: request.question,
options: request.options,
maxSelections: request.maxSelections,
durationSeconds: request.durationSeconds,
durationHours: request.durationHours
};
const threadId = typeof request.threadId === "string" && request.threadId.trim().length ? request.threadId.trim() : void 0;
const accountId = typeof request.accountId === "string" && request.accountId.trim().length ? request.accountId.trim() : void 0;
try {
const outbound = resolveOutboundChannelPlugin({
channel,
cfg
})?.outbound;
if (!outbound?.sendPoll) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unsupported poll channel: ${channel}`));
return;
}
const resolved = resolveOutboundTarget({
channel,
to,
cfg,
accountId,
mode: "explicit"
});
if (!resolved.ok) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, String(resolved.error)));
return;
}
const normalized = outbound.pollMaxOptions ? normalizePollInput(poll, { maxOptions: outbound.pollMaxOptions }) : normalizePollInput(poll);
const result = await outbound.sendPoll({
cfg,
to: resolved.to,
poll: normalized,
accountId,
threadId,
silent: request.silent,
isAnonymous: request.isAnonymous
});
const payload = {
runId: idem,
messageId: result.messageId,
channel
};
if (result.toJid) payload.toJid = result.toJid;
if (result.channelId) payload.channelId = result.channelId;
if (result.conversationId) payload.conversationId = result.conversationId;
if (result.pollId) payload.pollId = result.pollId;
context.dedupe.set(`poll:${idem}`, {
ts: Date.now(),
ok: true,
payload
});
respond(true, payload, void 0, { channel });
} catch (err) {
const error = errorShape(ErrorCodes.UNAVAILABLE, String(err));
context.dedupe.set(`poll:${idem}`, {
ts: Date.now(),
ok: false,
error
});
respond(false, void 0, error, {
channel,
error: formatForLog(err)
});
}
}
};
//#endregion
//#region src/gateway/sessions-patch.ts
function invalid(message) {
return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, message)
};
}
function normalizeExecHost(raw) {
const normalized = raw.trim().toLowerCase();
if (normalized === "sandbox" || normalized === "gateway" || normalized === "node") return normalized;
}
function normalizeExecSecurity(raw) {
const normalized = raw.trim().toLowerCase();
if (normalized === "deny" || normalized === "allowlist" || normalized === "full") return normalized;
}
function normalizeExecAsk(raw) {
const normalized = raw.trim().toLowerCase();
if (normalized === "off" || normalized === "on-miss" || normalized === "always") return normalized;
}
function supportsSpawnLineage(storeKey) {
return isSubagentSessionKey(storeKey) || isAcpSessionKey(storeKey);
}
function normalizeSubagentRole(raw) {
const normalized = raw.trim().toLowerCase();
if (normalized === "orchestrator" || normalized === "leaf") return normalized;
}
function normalizeSubagentControlScope(raw) {
const normalized = raw.trim().toLowerCase();
if (normalized === "children" || normalized === "none") return normalized;
}
async function applySessionsPatchToStore(params) {
const { cfg, store, storeKey, patch } = params;
const now = Date.now();
const sessionAgentId = normalizeAgentId(parseAgentSessionKey(storeKey)?.agentId ?? resolveDefaultAgentId(cfg));
const resolvedDefault = resolveDefaultModelForAgent({
cfg,
agentId: sessionAgentId
});
const subagentModelHint = isSubagentSessionKey(storeKey) ? resolveSubagentConfiguredModelSelection({
cfg,
agentId: sessionAgentId
}) : void 0;
const existing = store[storeKey];
const next = existing ? {
...existing,
updatedAt: Math.max(existing.updatedAt ?? 0, now)
} : {
sessionId: randomUUID(),
updatedAt: now
};
if ("spawnedBy" in patch) {
const raw = patch.spawnedBy;
if (raw === null) {
if (existing?.spawnedBy) return invalid("spawnedBy cannot be cleared once set");
} else if (raw !== void 0) {
const trimmed = String(raw).trim();
if (!trimmed) return invalid("invalid spawnedBy: empty");
if (!supportsSpawnLineage(storeKey)) return invalid("spawnedBy is only supported for subagent:* or acp:* sessions");
if (existing?.spawnedBy && existing.spawnedBy !== trimmed) return invalid("spawnedBy cannot be changed once set");
next.spawnedBy = trimmed;
}
}
if ("spawnedWorkspaceDir" in patch) {
const raw = patch.spawnedWorkspaceDir;
if (raw === null) {
if (existing?.spawnedWorkspaceDir) return invalid("spawnedWorkspaceDir cannot be cleared once set");
} else if (raw !== void 0) {
if (!supportsSpawnLineage(storeKey)) return invalid("spawnedWorkspaceDir is only supported for subagent:* or acp:* sessions");
const trimmed = String(raw).trim();
if (!trimmed) return invalid("invalid spawnedWorkspaceDir: empty");
if (existing?.spawnedWorkspaceDir && existing.spawnedWorkspaceDir !== trimmed) return invalid("spawnedWorkspaceDir cannot be changed once set");
next.spawnedWorkspaceDir = trimmed;
}
}
if ("spawnDepth" in patch) {
const raw = patch.spawnDepth;
if (raw === null) {
if (typeof existing?.spawnDepth === "number") return invalid("spawnDepth cannot be cleared once set");
} else if (raw !== void 0) {
if (!supportsSpawnLineage(storeKey)) return invalid("spawnDepth is only supported for subagent:* or acp:* sessions");
const numeric = Number(raw);
if (!Number.isInteger(numeric) || numeric < 0) return invalid("invalid spawnDepth (use an integer >= 0)");
const normalized = numeric;
if (typeof existing?.spawnDepth === "number" && existing.spawnDepth !== normalized) return invalid("spawnDepth cannot be changed once set");
next.spawnDepth = normalized;
}
}
if ("subagentRole" in patch) {
const raw = patch.subagentRole;
if (raw === null) {
if (existing?.subagentRole) return invalid("subagentRole cannot be cleared once set");
} else if (raw !== void 0) {
if (!supportsSpawnLineage(storeKey)) return invalid("subagentRole is only supported for subagent:* or acp:* sessions");
const normalized = normalizeSubagentRole(String(raw));
if (!normalized) return invalid("invalid subagentRole (use \"orchestrator\" or \"leaf\")");
if (existing?.subagentRole && existing.subagentRole !== normalized) return invalid("subagentRole cannot be changed once set");
next.subagentRole = normalized;
}
}
if ("subagentControlScope" in patch) {
const raw = patch.subagentControlScope;
if (raw === null) {
if (existing?.subagentControlScope) return invalid("subagentControlScope cannot be cleared once set");
} else if (raw !== void 0) {
if (!supportsSpawnLineage(storeKey)) return invalid("subagentControlScope is only supported for subagent:* or acp:* sessions");
const normalized = normalizeSubagentControlScope(String(raw));
if (!normalized) return invalid("invalid subagentControlScope (use \"children\" or \"none\")");
if (existing?.subagentControlScope && existing.subagentControlScope !== normalized) return invalid("subagentControlScope cannot be changed once set");
next.subagentControlScope = normalized;
}
}
if ("label" in patch) {
const raw = patch.label;
if (raw === null) delete next.label;
else if (raw !== void 0) {
const parsed = parseSessionLabel(raw);
if (!parsed.ok) return invalid(parsed.error);
for (const [key, entry] of Object.entries(store)) {
if (key === storeKey) continue;
if (entry?.label === parsed.label) return invalid(`label already in use: ${parsed.label}`);
}
next.label = parsed.label;
}
}
if ("thinkingLevel" in patch) {
const raw = patch.thinkingLevel;
if (raw === null) delete next.thinkingLevel;
else if (raw !== void 0) {
const normalized = normalizeThinkLevel(String(raw));
if (!normalized) return invalid(`invalid thinkingLevel (use ${formatThinkingLevels(existing?.providerOverride?.trim() || resolvedDefault.provider, existing?.modelOverride?.trim() || resolvedDefault.model, "|")})`);
next.thinkingLevel = normalized;
}
}
if ("fastMode" in patch) {
const raw = patch.fastMode;
if (raw === null) delete next.fastMode;
else if (raw !== void 0) {
const normalized = normalizeFastMode(raw);
if (normalized === void 0) return invalid("invalid fastMode (use true or false)");
next.fastMode = normalized;
}
}
if ("verboseLevel" in patch) {
const raw = patch.verboseLevel;
const parsed = parseVerboseOverride(raw);
if (!parsed.ok) return invalid(parsed.error);
applyVerboseOverride(next, parsed.value);
}
if ("reasoningLevel" in patch) {
const raw = patch.reasoningLevel;
if (raw === null) delete next.reasoningLevel;
else if (raw !== void 0) {
const normalized = normalizeReasoningLevel(String(raw));
if (!normalized) return invalid("invalid reasoningLevel (use \"on\"|\"off\"|\"stream\")");
next.reasoningLevel = normalized;
}
}
if ("responseUsage" in patch) {
const raw = patch.responseUsage;
if (raw === null) delete next.responseUsage;
else if (raw !== void 0) {
const normalized = normalizeUsageDisplay(String(raw));
if (!normalized) return invalid("invalid responseUsage (use \"off\"|\"tokens\"|\"full\")");
if (normalized === "off") delete next.responseUsage;
else next.responseUsage = normalized;
}
}
if ("elevatedLevel" in patch) {
const raw = patch.elevatedLevel;
if (raw === null) delete next.elevatedLevel;
else if (raw !== void 0) {
const normalized = normalizeElevatedLevel(String(raw));
if (!normalized) return invalid("invalid elevatedLevel (use \"on\"|\"off\"|\"ask\"|\"full\")");
next.elevatedLevel = normalized;
}
}
if ("execHost" in patch) {
const raw = patch.execHost;
if (raw === null) delete next.execHost;
else if (raw !== void 0) {
const normalized = normalizeExecHost(String(raw));
if (!normalized) return invalid("invalid execHost (use \"sandbox\"|\"gateway\"|\"node\")");
next.execHost = normalized;
}
}
if ("execSecurity" in patch) {
const raw = patch.execSecurity;
if (raw === null) delete next.execSecurity;
else if (raw !== void 0) {
const normalized = normalizeExecSecurity(String(raw));
if (!normalized) return invalid("invalid execSecurity (use \"deny\"|\"allowlist\"|\"full\")");
next.execSecurity = normalized;
}
}
if ("execAsk" in patch) {
const raw = patch.execAsk;
if (raw === null) delete next.execAsk;
else if (raw !== void 0) {
const normalized = normalizeExecAsk(String(raw));
if (!normalized) return invalid("invalid execAsk (use \"off\"|\"on-miss\"|\"always\")");
next.execAsk = normalized;
}
}
if ("execNode" in patch) {
const raw = patch.execNode;
if (raw === null) delete next.execNode;
else if (raw !== void 0) {
const trimmed = String(raw).trim();
if (!trimmed) return invalid("invalid execNode: empty");
next.execNode = trimmed;
}
}
if ("model" in patch) {
const raw = patch.model;
if (raw === null) applyModelOverrideToSessionEntry({
entry: next,
selection: {
provider: resolvedDefault.provider,
model: resolvedDefault.model,
isDefault: true
}
});
else if (raw !== void 0) {
const trimmed = String(raw).trim();
if (!trimmed) return invalid("invalid model: empty");
if (!params.loadGatewayModelCatalog) return {
ok: false,
error: errorShape(ErrorCodes.UNAVAILABLE, "model catalog unavailable")
};
const resolved = resolveAllowedModelRef({
cfg,
catalog: await params.loadGatewayModelCatalog(),
raw: trimmed,
defaultProvider: resolvedDefault.provider,
defaultModel: subagentModelHint ?? resolvedDefault.model
});
if ("error" in resolved) return invalid(resolved.error);
const isDefault = resolved.ref.provider === resolvedDefault.provider && resolved.ref.model === resolvedDefault.model;
applyModelOverrideToSessionEntry({
entry: next,
selection: {
provider: resolved.ref.provider,
model: resolved.ref.model,
isDefault
}
});
}
}
if (next.thinkingLevel === "xhigh") {
if (!supportsXHighThinking(next.providerOverride ?? resolvedDefault.provider, next.modelOverride ?? resolvedDefault.model)) {
if ("thinkingLevel" in patch) return invalid(`thinkingLevel "xhigh" is only supported for ${formatXHighModelHint()}`);
next.thinkingLevel = "high";
}
}
if ("sendPolicy" in patch) {
const raw = patch.sendPolicy;
if (raw === null) delete next.sendPolicy;
else if (raw !== void 0) {
const normalized = normalizeSendPolicy(String(raw));
if (!normalized) return invalid("invalid sendPolicy (use \"allow\"|\"deny\")");
next.sendPolicy = normalized;
}
}
if ("groupActivation" in patch) {
const raw = patch.groupActivation;
if (raw === null) delete next.groupActivation;
else if (raw !== void 0) {
const normalized = normalizeGroupActivation(String(raw));
if (!normalized) return invalid("invalid groupActivation (use \"mention\"|\"always\")");
next.groupActivation = normalized;
}
}
store[storeKey] = next;
return {
ok: true,
entry: next
};
}
//#endregion
//#region src/gateway/sessions-resolve.ts
async function resolveSessionKeyFromResolveParams(params) {
const { cfg, p } = params;
const key = typeof p.key === "string" ? p.key.trim() : "";
const hasKey = key.length > 0;
const sessionId = typeof p.sessionId === "string" ? p.sessionId.trim() : "";
const hasSessionId = sessionId.length > 0;
const selectionCount = [
hasKey,
hasSessionId,
typeof p.label === "string" && p.label.trim().length > 0
].filter(Boolean).length;
if (selectionCount > 1) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, "Provide either key, sessionId, or label (not multiple)")
};
if (selectionCount === 0) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, "Either key, sessionId, or label is required")
};
if (hasKey) {
const target = resolveGatewaySessionStoreTarget({
cfg,
key
});
const store = loadSessionStore(target.storePath);
if (store[target.canonicalKey]) return {
ok: true,
key: target.canonicalKey
};
const legacyKey = target.storeKeys.find((candidate) => store[candidate]);
if (!legacyKey) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `No session found: ${key}`)
};
await updateSessionStore(target.storePath, (s) => {
const { primaryKey } = migrateAndPruneGatewaySessionStoreKey({
cfg,
key,
store: s
});
if (!s[primaryKey] && s[legacyKey]) s[primaryKey] = s[legacyKey];
});
return {
ok: true,
key: target.canonicalKey
};
}
if (hasSessionId) {
const { storePath, store } = loadCombinedSessionStoreForGateway(cfg);
const matches = listSessionsFromStore({
cfg,
storePath,
store,
opts: {
includeGlobal: p.includeGlobal === true,
includeUnknown: p.includeUnknown === true,
spawnedBy: p.spawnedBy,
agentId: p.agentId,
search: sessionId,
limit: 8
}
}).sessions.filter((session) => session.sessionId === sessionId || session.key === sessionId);
if (matches.length === 0) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `No session found: ${sessionId}`)
};
if (matches.length > 1) {
const keys = matches.map((session) => session.key).join(", ");
return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `Multiple sessions found for sessionId: ${sessionId} (${keys})`)
};
}
return {
ok: true,
key: String(matches[0]?.key ?? "")
};
}
const parsedLabel = parseSessionLabel(p.label);
if (!parsedLabel.ok) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, parsedLabel.error)
};
const { storePath, store } = loadCombinedSessionStoreForGateway(cfg);
const list = listSessionsFromStore({
cfg,
storePath,
store,
opts: {
includeGlobal: p.includeGlobal === true,
includeUnknown: p.includeUnknown === true,
label: parsedLabel.label,
agentId: p.agentId,
spawnedBy: p.spawnedBy,
limit: 2
}
});
if (list.sessions.length === 0) return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `No session found with label: ${parsedLabel.label}`)
};
if (list.sessions.length > 1) {
const keys = list.sessions.map((s) => s.key).join(", ");
return {
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `Multiple sessions found with label: ${parsedLabel.label} (${keys})`)
};
}
return {
ok: true,
key: String(list.sessions[0]?.key ?? "")
};
}
//#endregion
//#region src/gateway/server-methods/sessions.ts
function requireSessionKey(key, respond) {
const normalized = (typeof key === "string" ? key : typeof key === "number" ? String(key) : typeof key === "bigint" ? String(key) : "").trim();
if (!normalized) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "key required"));
return null;
}
return normalized;
}
function resolveGatewaySessionTargetFromKey(key) {
const cfg = loadConfig();
const target = resolveGatewaySessionStoreTarget({
cfg,
key
});
return {
cfg,
target,
storePath: target.storePath
};
}
function resolveOptionalInitialSessionMessage(params) {
if (typeof params.task === "string" && params.task.trim()) return params.task;
if (typeof params.message === "string" && params.message.trim()) return params.message;
}
function shouldAttachPendingMessageSeq(params) {
if (params.cached) return false;
return (params.payload && typeof params.payload === "object" ? params.payload.status : void 0) === "started";
}
function emitSessionsChanged(context, payload) {
const connIds = context.getSessionEventSubscriberConnIds();
if (connIds.size === 0) return;
const sessionRow = payload.sessionKey ? loadGatewaySessionRow(payload.sessionKey) : null;
context.broadcastToConnIds("sessions.changed", {
...payload,
ts: Date.now(),
...sessionRow ? {
updatedAt: sessionRow.updatedAt ?? void 0,
sessionId: sessionRow.sessionId,
kind: sessionRow.kind,
channel: sessionRow.channel,
label: sessionRow.label,
displayName: sessionRow.displayName,
deliveryContext: sessionRow.deliveryContext,
parentSessionKey: sessionRow.parentSessionKey,
childSessions: sessionRow.childSessions,
thinkingLevel: sessionRow.thinkingLevel,
systemSent: sessionRow.systemSent,
abortedLastRun: sessionRow.abortedLastRun,
lastChannel: sessionRow.lastChannel,
lastTo: sessionRow.lastTo,
lastAccountId: sessionRow.lastAccountId,
totalTokens: sessionRow.totalTokens,
totalTokensFresh: sessionRow.totalTokensFresh,
contextTokens: sessionRow.contextTokens,
estimatedCostUsd: sessionRow.estimatedCostUsd,
modelProvider: sessionRow.modelProvider,
model: sessionRow.model,
status: sessionRow.status,
startedAt: sessionRow.startedAt,
endedAt: sessionRow.endedAt,
runtimeMs: sessionRow.runtimeMs
} : {}
}, connIds, { dropIfSlow: true });
}
function rejectWebchatSessionMutation(params) {
if (!params.client?.connect || !params.isWebchatConnect(params.client.connect)) return false;
if (params.client.connect.client.id === GATEWAY_CLIENT_IDS.CONTROL_UI) return false;
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `webchat clients cannot ${params.action} sessions; use chat.send for session-scoped updates`));
return true;
}
function buildDashboardSessionKey(agentId) {
return `agent:${agentId}:dashboard:${randomUUID()}`;
}
function ensureSessionTranscriptFile(params) {
try {
const transcriptPath = resolveSessionFilePath(params.sessionId, params.sessionFile ? { sessionFile: params.sessionFile } : void 0, resolveSessionFilePathOptions({
storePath: params.storePath,
agentId: params.agentId
}));
if (!fs.existsSync(transcriptPath)) {
fs.mkdirSync(path.dirname(transcriptPath), { recursive: true });
const header = {
type: "session",
version: CURRENT_SESSION_VERSION,
id: params.sessionId,
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
cwd: process.cwd()
};
fs.writeFileSync(transcriptPath, `${JSON.stringify(header)}\n`, {
encoding: "utf-8",
mode: 384
});
}
return {
ok: true,
transcriptPath
};
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err)
};
}
}
function resolveAbortSessionKey(params) {
const activeRunKey = typeof params.runId === "string" ? params.context.chatAbortControllers.get(params.runId)?.sessionKey : void 0;
if (activeRunKey) return activeRunKey;
for (const active of params.context.chatAbortControllers.values()) {
if (active.sessionKey === params.canonicalKey) return params.canonicalKey;
if (active.sessionKey === params.requestedKey) return params.requestedKey;
}
return params.requestedKey;
}
function hasTrackedActiveSessionRun(params) {
for (const active of params.context.chatAbortControllers.values()) if (active.sessionKey === params.canonicalKey || active.sessionKey === params.requestedKey) return true;
return false;
}
async function interruptSessionRunIfActive(params) {
const hasTrackedRun = hasTrackedActiveSessionRun({
context: params.context,
requestedKey: params.requestedKey,
canonicalKey: params.canonicalKey
});
const hasEmbeddedRun = typeof params.sessionId === "string" && params.sessionId ? isEmbeddedPiRunActive(params.sessionId) : false;
if (!hasTrackedRun && !hasEmbeddedRun) return { interrupted: false };
if (hasTrackedRun) {
let abortOk = true;
let abortError;
const abortSessionKey = resolveAbortSessionKey({
context: params.context,
requestedKey: params.requestedKey,
canonicalKey: params.canonicalKey
});
await chatHandlers["chat.abort"]({
req: params.req,
params: { sessionKey: abortSessionKey },
respond: (ok, _payload, error) => {
abortOk = ok;
abortError = error;
},
context: params.context,
client: params.client,
isWebchatConnect: params.isWebchatConnect
});
if (!abortOk) return {
interrupted: true,
error: abortError ?? errorShape(ErrorCodes.UNAVAILABLE, "failed to interrupt active session")
};
}
if (hasEmbeddedRun && params.sessionId) abortEmbeddedPiRun(params.sessionId);
clearSessionQueues([
params.requestedKey,
params.canonicalKey,
params.sessionId
]);
if (hasEmbeddedRun && params.sessionId) {
if (!await waitForEmbeddedPiRunEnd(params.sessionId, 15e3)) return {
interrupted: true,
error: errorShape(ErrorCodes.UNAVAILABLE, `Session ${params.requestedKey} is still active; try again in a moment.`)
};
}
return { interrupted: true };
}
async function handleSessionSend(params) {
if (!assertValidParams(params.params, validateSessionsSendParams, params.method, params.respond)) return;
const p = params.params;
const key = requireSessionKey(p.key, params.respond);
if (!key) return;
const { entry, canonicalKey, storePath } = loadSessionEntry(key);
if (!entry?.sessionId) {
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `session not found: ${key}`));
return;
}
let interruptedActiveRun = false;
if (params.interruptIfActive) {
const interruptResult = await interruptSessionRunIfActive({
req: params.req,
context: params.context,
client: params.client,
isWebchatConnect: params.isWebchatConnect,
requestedKey: key,
canonicalKey,
sessionId: entry.sessionId
});
if (interruptResult.error) {
params.respond(false, void 0, interruptResult.error);
return;
}
interruptedActiveRun = interruptResult.interrupted;
}
const messageSeq = readSessionMessages(entry.sessionId, storePath, entry.sessionFile).length + 1;
let sendAcked = false;
let sendPayload;
let sendCached = false;
let startedRunId;
const rawIdempotencyKey = p.idempotencyKey;
const idempotencyKey = typeof rawIdempotencyKey === "string" && rawIdempotencyKey.trim() ? rawIdempotencyKey.trim() : randomUUID();
await chatHandlers["chat.send"]({
req: params.req,
params: {
sessionKey: canonicalKey,
message: p.message,
thinking: p.thinking,
attachments: p.attachments,
timeoutMs: p.timeoutMs,
idempotencyKey
},
respond: (ok, payload, error, meta) => {
sendAcked = ok;
sendPayload = payload;
sendCached = meta?.cached === true;
startedRunId = payload && typeof payload === "object" && typeof payload.runId === "string" ? payload.runId : void 0;
if (ok && shouldAttachPendingMessageSeq({
payload,
cached: meta?.cached === true
})) {
params.respond(true, {
...payload && typeof payload === "object" ? payload : {},
messageSeq,
...interruptedActiveRun ? { interruptedActiveRun: true } : {}
}, void 0, meta);
return;
}
params.respond(ok, ok && payload && typeof payload === "object" ? {
...payload,
...interruptedActiveRun ? { interruptedActiveRun: true } : {}
} : payload, error, meta);
},
context: params.context,
client: params.client,
isWebchatConnect: params.isWebchatConnect
});
if (sendAcked) {
if (shouldAttachPendingMessageSeq({
payload: sendPayload,
cached: sendCached
})) reactivateCompletedSubagentSession({
sessionKey: canonicalKey,
runId: startedRunId
});
emitSessionsChanged(params.context, {
sessionKey: canonicalKey,
reason: interruptedActiveRun ? "steer" : "send"
});
}
}
const sessionsHandlers = {
"sessions.list": ({ params, respond }) => {
if (!assertValidParams(params, validateSessionsListParams, "sessions.list", respond)) return;
const p = params;
const cfg = loadConfig();
const { storePath, store } = loadCombinedSessionStoreForGateway(cfg);
respond(true, listSessionsFromStore({
cfg,
storePath,
store,
opts: p
}), void 0);
},
"sessions.subscribe": ({ client, context, respond }) => {
const connId = client?.connId?.trim();
if (connId) context.subscribeSessionEvents(connId);
respond(true, { subscribed: Boolean(connId) }, void 0);
},
"sessions.unsubscribe": ({ client, context, respond }) => {
const connId = client?.connId?.trim();
if (connId) context.unsubscribeSessionEvents(connId);
respond(true, { subscribed: false }, void 0);
},
"sessions.messages.subscribe": ({ params, client, context, respond }) => {
if (!assertValidParams(params, validateSessionsMessagesSubscribeParams, "sessions.messages.subscribe", respond)) return;
const connId = client?.connId?.trim();
const key = requireSessionKey(params.key, respond);
if (!key) return;
const { canonicalKey } = loadSessionEntry(key);
if (connId) {
context.subscribeSessionMessageEvents(connId, canonicalKey);
respond(true, {
subscribed: true,
key: canonicalKey
}, void 0);
return;
}
respond(true, {
subscribed: false,
key: canonicalKey
}, void 0);
},
"sessions.messages.unsubscribe": ({ params, client, context, respond }) => {
if (!assertValidParams(params, validateSessionsMessagesUnsubscribeParams, "sessions.messages.unsubscribe", respond)) return;
const connId = client?.connId?.trim();
const key = requireSessionKey(params.key, respond);
if (!key) return;
const { canonicalKey } = loadSessionEntry(key);
if (connId) context.unsubscribeSessionMessageEvents(connId, canonicalKey);
respond(true, {
subscribed: false,
key: canonicalKey
}, void 0);
},
"sessions.preview": ({ params, respond }) => {
if (!assertValidParams(params, validateSessionsPreviewParams, "sessions.preview", respond)) return;
const p = params;
const keys = (Array.isArray(p.keys) ? p.keys : []).map((key) => String(key ?? "").trim()).filter(Boolean).slice(0, 64);
const limit = typeof p.limit === "number" && Number.isFinite(p.limit) ? Math.max(1, p.limit) : 12;
const maxChars = typeof p.maxChars === "number" && Number.isFinite(p.maxChars) ? Math.max(20, p.maxChars) : 240;
if (keys.length === 0) {
respond(true, {
ts: Date.now(),
previews: []
}, void 0);
return;
}
const cfg = loadConfig();
const storeCache = /* @__PURE__ */ new Map();
const previews = [];
for (const key of keys) try {
const storeTarget = resolveGatewaySessionStoreTarget({
cfg,
key,
scanLegacyKeys: false
});
const store = storeCache.get(storeTarget.storePath) ?? loadSessionStore(storeTarget.storePath);
storeCache.set(storeTarget.storePath, store);
const target = resolveGatewaySessionStoreTarget({
cfg,
key,
store
});
const entry = target.storeKeys.map((candidate) => store[candidate]).find(Boolean);
if (!entry?.sessionId) {
previews.push({
key,
status: "missing",
items: []
});
continue;
}
const items = readSessionPreviewItemsFromTranscript(entry.sessionId, target.storePath, entry.sessionFile, target.agentId, limit, maxChars);
previews.push({
key,
status: items.length > 0 ? "ok" : "empty",
items
});
} catch {
previews.push({
key,
status: "error",
items: []
});
}
respond(true, {
ts: Date.now(),
previews
}, void 0);
},
"sessions.resolve": async ({ params, respond }) => {
if (!assertValidParams(params, validateSessionsResolveParams, "sessions.resolve", respond)) return;
const p = params;
const resolved = await resolveSessionKeyFromResolveParams({
cfg: loadConfig(),
p
});
if (!resolved.ok) {
respond(false, void 0, resolved.error);
return;
}
respond(true, {
ok: true,
key: resolved.key
}, void 0);
},
"sessions.create": async ({ req, params, respond, context, client, isWebchatConnect }) => {
if (!assertValidParams(params, validateSessionsCreateParams, "sessions.create", respond)) return;
const p = params;
const cfg = loadConfig();
const requestedKey = typeof p.key === "string" && p.key.trim() ? p.key.trim() : void 0;
const agentId = normalizeAgentId(typeof p.agentId === "string" && p.agentId.trim() ? p.agentId : resolveDefaultAgentId(cfg));
if (requestedKey) {
const requestedAgentId = parseAgentSessionKey(requestedKey)?.agentId;
if (requestedAgentId && requestedAgentId !== agentId && typeof p.agentId === "string" && p.agentId.trim()) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `sessions.create key agent (${requestedAgentId}) does not match agentId (${agentId})`));
return;
}
}
const parentSessionKey = typeof p.parentSessionKey === "string" && p.parentSessionKey.trim() ? p.parentSessionKey.trim() : void 0;
let canonicalParentSessionKey;
if (parentSessionKey) {
const parent = loadSessionEntry(parentSessionKey);
if (!parent.entry?.sessionId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unknown parent session: ${parentSessionKey}`));
return;
}
canonicalParentSessionKey = parent.canonicalKey;
}
const target = resolveGatewaySessionStoreTarget({
cfg,
key: requestedKey ?? buildDashboardSessionKey(agentId)
});
const targetAgentId = resolveAgentIdFromSessionKey(target.canonicalKey);
const created = await updateSessionStore(target.storePath, async (store) => {
const patched = await applySessionsPatchToStore({
cfg,
store,
storeKey: target.canonicalKey,
patch: {
key: target.canonicalKey,
label: typeof p.label === "string" ? p.label.trim() : void 0,
model: typeof p.model === "string" ? p.model.trim() : void 0
},
loadGatewayModelCatalog: context.loadGatewayModelCatalog
});
if (!patched.ok || !canonicalParentSessionKey) return patched;
const nextEntry = {
...patched.entry,
parentSessionKey: canonicalParentSessionKey
};
store[target.canonicalKey] = nextEntry;
return {
...patched,
entry: nextEntry
};
});
if (!created.ok) {
respond(false, void 0, created.error);
return;
}
const ensured = ensureSessionTranscriptFile({
sessionId: created.entry.sessionId,
storePath: target.storePath,
sessionFile: created.entry.sessionFile,
agentId: targetAgentId
});
if (!ensured.ok) {
await updateSessionStore(target.storePath, (store) => {
delete store[target.canonicalKey];
});
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, `failed to create session transcript: ${ensured.error}`));
return;
}
const initialMessage = resolveOptionalInitialSessionMessage(p);
let runPayload;
let runError;
let runMeta;
const messageSeq = initialMessage ? readSessionMessages(created.entry.sessionId, target.storePath, created.entry.sessionFile).length + 1 : void 0;
if (initialMessage) await chatHandlers["chat.send"]({
req,
params: {
sessionKey: target.canonicalKey,
message: initialMessage,
idempotencyKey: randomUUID()
},
respond: (ok, payload, error, meta) => {
if (ok && payload && typeof payload === "object") runPayload = payload;
else runError = error;
runMeta = meta;
},
context,
client,
isWebchatConnect
});
const runStarted = runPayload !== void 0 && shouldAttachPendingMessageSeq({
payload: runPayload,
cached: runMeta?.cached === true
});
respond(true, {
ok: true,
key: target.canonicalKey,
sessionId: created.entry.sessionId,
entry: created.entry,
runStarted,
...runPayload ? runPayload : {},
...runStarted && typeof messageSeq === "number" ? { messageSeq } : {},
...runError ? { runError } : {}
}, void 0);
emitSessionsChanged(context, {
sessionKey: target.canonicalKey,
reason: "create"
});
if (runStarted) emitSessionsChanged(context, {
sessionKey: target.canonicalKey,
reason: "send"
});
},
"sessions.send": async ({ req, params, respond, context, client, isWebchatConnect }) => {
await handleSessionSend({
method: "sessions.send",
req,
params,
respond,
context,
client,
isWebchatConnect,
interruptIfActive: false
});
},
"sessions.steer": async ({ req, params, respond, context, client, isWebchatConnect }) => {
await handleSessionSend({
method: "sessions.steer",
req,
params,
respond,
context,
client,
isWebchatConnect,
interruptIfActive: true
});
},
"sessions.abort": async ({ req, params, respond, context, client, isWebchatConnect }) => {
if (!assertValidParams(params, validateSessionsAbortParams, "sessions.abort", respond)) return;
const p = params;
const key = requireSessionKey(p.key, respond);
if (!key) return;
const { canonicalKey } = loadSessionEntry(key);
const abortSessionKey = resolveAbortSessionKey({
context,
requestedKey: key,
canonicalKey,
runId: typeof p.runId === "string" ? p.runId : void 0
});
let abortedRunId = null;
await chatHandlers["chat.abort"]({
req,
params: {
sessionKey: abortSessionKey,
runId: typeof p.runId === "string" ? p.runId : void 0
},
respond: (ok, payload, error, meta) => {
if (!ok) {
respond(ok, payload, error, meta);
return;
}
abortedRunId = (payload && typeof payload === "object" && Array.isArray(payload.runIds) ? payload.runIds.filter((value) => typeof value === "string" && value.trim().length > 0) : [])[0] ?? null;
respond(true, {
ok: true,
abortedRunId,
status: abortedRunId ? "aborted" : "no-active-run"
}, void 0, meta);
},
context,
client,
isWebchatConnect
});
if (abortedRunId) emitSessionsChanged(context, {
sessionKey: canonicalKey,
reason: "abort"
});
},
"sessions.patch": async ({ params, respond, context, client, isWebchatConnect }) => {
if (!assertValidParams(params, validateSessionsPatchParams, "sessions.patch", respond)) return;
const p = params;
const key = requireSessionKey(p.key, respond);
if (!key) return;
if (rejectWebchatSessionMutation({
action: "patch",
client,
isWebchatConnect,
respond
})) return;
const { cfg, target, storePath } = resolveGatewaySessionTargetFromKey(key);
const applied = await updateSessionStore(storePath, async (store) => {
const { primaryKey } = migrateAndPruneGatewaySessionStoreKey({
cfg,
key,
store
});
return await applySessionsPatchToStore({
cfg,
store,
storeKey: primaryKey,
patch: p,
loadGatewayModelCatalog: context.loadGatewayModelCatalog
});
});
if (!applied.ok) {
respond(false, void 0, applied.error);
return;
}
const agentId = normalizeAgentId(parseAgentSessionKey(target.canonicalKey ?? key)?.agentId ?? resolveDefaultAgentId(cfg));
const resolved = resolveSessionModelRef(cfg, applied.entry, agentId);
respond(true, {
ok: true,
path: storePath,
key: target.canonicalKey,
entry: applied.entry,
resolved: {
modelProvider: resolved.provider,
model: resolved.model
}
}, void 0);
emitSessionsChanged(context, {
sessionKey: target.canonicalKey,
reason: "patch"
});
},
"sessions.reset": async ({ params, respond, context }) => {
if (!assertValidParams(params, validateSessionsResetParams, "sessions.reset", respond)) return;
const p = params;
const key = requireSessionKey(p.key, respond);
if (!key) return;
const reason = p.reason === "new" ? "new" : "reset";
const result = await performGatewaySessionReset({
key,
reason,
commandSource: "gateway:sessions.reset"
});
if (!result.ok) {
respond(false, void 0, result.error);
return;
}
respond(true, {
ok: true,
key: result.key,
entry: result.entry
}, void 0);
emitSessionsChanged(context, {
sessionKey: result.key,
reason
});
},
"sessions.delete": async ({ params, respond, client, isWebchatConnect, context }) => {
if (!assertValidParams(params, validateSessionsDeleteParams, "sessions.delete", respond)) return;
const p = params;
const key = requireSessionKey(p.key, respond);
if (!key) return;
if (rejectWebchatSessionMutation({
action: "delete",
client,
isWebchatConnect,
respond
})) return;
const { cfg, target, storePath } = resolveGatewaySessionTargetFromKey(key);
const mainKey = resolveMainSessionKey(cfg);
if (target.canonicalKey === mainKey) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `Cannot delete the main session (${mainKey}).`));
return;
}
const deleteTranscript = typeof p.deleteTranscript === "boolean" ? p.deleteTranscript : true;
const { entry, legacyKey, canonicalKey } = loadSessionEntry(key);
const mutationCleanupError = await cleanupSessionBeforeMutation({
cfg,
key,
target,
entry,
legacyKey,
canonicalKey,
reason: "session-delete"
});
if (mutationCleanupError) {
respond(false, void 0, mutationCleanupError);
return;
}
const sessionId = entry?.sessionId;
const deleted = await updateSessionStore(storePath, (store) => {
const { primaryKey } = migrateAndPruneGatewaySessionStoreKey({
cfg,
key,
store
});
const hadEntry = Boolean(store[primaryKey]);
if (hadEntry) delete store[primaryKey];
return hadEntry;
});
const archived = deleted && deleteTranscript ? archiveSessionTranscriptsForSession({
sessionId,
storePath,
sessionFile: entry?.sessionFile,
agentId: target.agentId,
reason: "deleted"
}) : [];
if (deleted) {
const emitLifecycleHooks = p.emitLifecycleHooks !== false;
await emitSessionUnboundLifecycleEvent({
targetSessionKey: target.canonicalKey ?? key,
reason: "session-delete",
emitHooks: emitLifecycleHooks
});
}
respond(true, {
ok: true,
key: target.canonicalKey,
deleted,
archived
}, void 0);
if (deleted) emitSessionsChanged(context, {
sessionKey: target.canonicalKey,
reason: "delete"
});
},
"sessions.get": ({ params, respond }) => {
const p = params;
const key = requireSessionKey(p.key ?? p.sessionKey, respond);
if (!key) return;
const limit = typeof p.limit === "number" && Number.isFinite(p.limit) ? Math.max(1, Math.floor(p.limit)) : 200;
const { target, storePath } = resolveGatewaySessionTargetFromKey(key);
const store = loadSessionStore(storePath);
const entry = target.storeKeys.map((k) => store[k]).find(Boolean);
if (!entry?.sessionId) {
respond(true, { messages: [] }, void 0);
return;
}
const allMessages = readSessionMessages(entry.sessionId, storePath, entry.sessionFile);
respond(true, { messages: limit < allMessages.length ? allMessages.slice(-limit) : allMessages }, void 0);
},
"sessions.compact": async ({ params, respond, context }) => {
if (!assertValidParams(params, validateSessionsCompactParams, "sessions.compact", respond)) return;
const p = params;
const key = requireSessionKey(p.key, respond);
if (!key) return;
const maxLines = typeof p.maxLines === "number" && Number.isFinite(p.maxLines) ? Math.max(1, Math.floor(p.maxLines)) : 400;
const { cfg, target, storePath } = resolveGatewaySessionTargetFromKey(key);
const compactTarget = await updateSessionStore(storePath, (store) => {
const { entry, primaryKey } = migrateAndPruneGatewaySessionStoreKey({
cfg,
key,
store
});
return {
entry,
primaryKey
};
});
const entry = compactTarget.entry;
const sessionId = entry?.sessionId;
if (!sessionId) {
respond(true, {
ok: true,
key: target.canonicalKey,
compacted: false,
reason: "no sessionId"
}, void 0);
return;
}
const filePath = resolveSessionTranscriptCandidates(sessionId, storePath, entry?.sessionFile, target.agentId).find((candidate) => fs.existsSync(candidate));
if (!filePath) {
respond(true, {
ok: true,
key: target.canonicalKey,
compacted: false,
reason: "no transcript"
}, void 0);
return;
}
const lines = fs.readFileSync(filePath, "utf-8").split(/\r?\n/).filter((l) => l.trim().length > 0);
if (lines.length <= maxLines) {
respond(true, {
ok: true,
key: target.canonicalKey,
compacted: false,
kept: lines.length
}, void 0);
return;
}
const archived = archiveFileOnDisk(filePath, "bak");
const keptLines = lines.slice(-maxLines);
fs.writeFileSync(filePath, `${keptLines.join("\n")}\n`, "utf-8");
await updateSessionStore(storePath, (store) => {
const entryToUpdate = store[compactTarget.primaryKey];
if (!entryToUpdate) return;
delete entryToUpdate.inputTokens;
delete entryToUpdate.outputTokens;
delete entryToUpdate.totalTokens;
delete entryToUpdate.totalTokensFresh;
entryToUpdate.updatedAt = Date.now();
});
respond(true, {
ok: true,
key: target.canonicalKey,
compacted: true,
archived,
kept: keptLines.length
}, void 0);
emitSessionsChanged(context, {
sessionKey: target.canonicalKey,
reason: "compact",
compacted: true
});
}
};
//#endregion
//#region src/gateway/server-methods/skills.ts
function collectSkillBins(entries) {
const bins = /* @__PURE__ */ new Set();
for (const entry of entries) {
const required = entry.metadata?.requires?.bins ?? [];
const anyBins = entry.metadata?.requires?.anyBins ?? [];
const install = entry.metadata?.install ?? [];
for (const bin of required) {
const trimmed = bin.trim();
if (trimmed) bins.add(trimmed);
}
for (const bin of anyBins) {
const trimmed = bin.trim();
if (trimmed) bins.add(trimmed);
}
for (const spec of install) {
const specBins = spec?.bins ?? [];
for (const bin of specBins) {
const trimmed = String(bin).trim();
if (trimmed) bins.add(trimmed);
}
}
}
return [...bins].toSorted();
}
const skillsHandlers = {
"skills.status": ({ params, respond }) => {
if (!validateSkillsStatusParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid skills.status params: ${formatValidationErrors(validateSkillsStatusParams.errors)}`));
return;
}
const cfg = loadConfig();
const agentIdRaw = typeof params?.agentId === "string" ? params.agentId.trim() : "";
const agentId = agentIdRaw ? normalizeAgentId(agentIdRaw) : resolveDefaultAgentId(cfg);
if (agentIdRaw) {
if (!listAgentIds(cfg).includes(agentId)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unknown agent id "${agentIdRaw}"`));
return;
}
}
respond(true, buildWorkspaceSkillStatus(resolveAgentWorkspaceDir(cfg, agentId), {
config: cfg,
eligibility: { remote: getRemoteSkillEligibility() }
}), void 0);
},
"skills.bins": ({ params, respond }) => {
if (!validateSkillsBinsParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid skills.bins params: ${formatValidationErrors(validateSkillsBinsParams.errors)}`));
return;
}
const cfg = loadConfig();
const workspaceDirs = listAgentWorkspaceDirs(cfg);
const bins = /* @__PURE__ */ new Set();
for (const workspaceDir of workspaceDirs) {
const entries = loadWorkspaceSkillEntries(workspaceDir, { config: cfg });
for (const bin of collectSkillBins(entries)) bins.add(bin);
}
respond(true, { bins: [...bins].toSorted() }, void 0);
},
"skills.install": async ({ params, respond }) => {
if (!validateSkillsInstallParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid skills.install params: ${formatValidationErrors(validateSkillsInstallParams.errors)}`));
return;
}
const cfg = loadConfig();
const workspaceDirRaw = resolveAgentWorkspaceDir(cfg, resolveDefaultAgentId(cfg));
if (params && typeof params === "object" && "source" in params && params.source === "clawhub") {
const p = params;
const result = await installSkillFromClawHub({
workspaceDir: workspaceDirRaw,
slug: p.slug,
version: p.version,
force: Boolean(p.force)
});
respond(result.ok, result.ok ? {
ok: true,
message: `Installed ${result.slug}@${result.version}`,
stdout: "",
stderr: "",
code: 0,
slug: result.slug,
version: result.version,
targetDir: result.targetDir
} : result, result.ok ? void 0 : errorShape(ErrorCodes.UNAVAILABLE, result.error));
return;
}
const p = params;
const result = await installSkill({
workspaceDir: workspaceDirRaw,
skillName: p.name,
installId: p.installId,
timeoutMs: p.timeoutMs,
config: cfg
});
respond(result.ok, result, result.ok ? void 0 : errorShape(ErrorCodes.UNAVAILABLE, result.message));
},
"skills.update": async ({ params, respond }) => {
if (!validateSkillsUpdateParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid skills.update params: ${formatValidationErrors(validateSkillsUpdateParams.errors)}`));
return;
}
if (params && typeof params === "object" && "source" in params && params.source === "clawhub") {
const p = params;
if (!p.slug && !p.all) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "clawhub skills.update requires \"slug\" or \"all\""));
return;
}
if (p.slug && p.all) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "clawhub skills.update accepts either \"slug\" or \"all\", not both"));
return;
}
const cfg = loadConfig();
const results = await updateSkillsFromClawHub({
workspaceDir: resolveAgentWorkspaceDir(cfg, resolveDefaultAgentId(cfg)),
slug: p.slug
});
const errors = results.filter((result) => !result.ok);
respond(errors.length === 0, {
ok: errors.length === 0,
skillKey: p.slug ?? "*",
config: {
source: "clawhub",
results
}
}, errors.length === 0 ? void 0 : errorShape(ErrorCodes.UNAVAILABLE, errors.map((result) => result.error).join("; ")));
return;
}
const p = params;
const cfg = loadConfig();
const skills = cfg.skills ? { ...cfg.skills } : {};
const entries = skills.entries ? { ...skills.entries } : {};
const current = entries[p.skillKey] ? { ...entries[p.skillKey] } : {};
if (typeof p.enabled === "boolean") current.enabled = p.enabled;
if (typeof p.apiKey === "string") {
const trimmed = normalizeSecretInput(p.apiKey);
if (trimmed) current.apiKey = trimmed;
else delete current.apiKey;
}
if (p.env && typeof p.env === "object") {
const nextEnv = current.env ? { ...current.env } : {};
for (const [key, value] of Object.entries(p.env)) {
const trimmedKey = key.trim();
if (!trimmedKey) continue;
const trimmedVal = value.trim();
if (!trimmedVal) delete nextEnv[trimmedKey];
else nextEnv[trimmedKey] = trimmedVal;
}
current.env = nextEnv;
}
entries[p.skillKey] = current;
skills.entries = entries;
await writeConfigFile({
...cfg,
skills
});
respond(true, {
ok: true,
skillKey: p.skillKey,
config: current
}, void 0);
}
};
//#endregion
//#region src/gateway/server/presence-events.ts
function broadcastPresenceSnapshot(params) {
const presenceVersion = params.incrementPresenceVersion();
params.broadcast("presence", { presence: listSystemPresence() }, {
dropIfSlow: true,
stateVersion: {
presence: presenceVersion,
health: params.getHealthVersion()
}
});
return presenceVersion;
}
//#endregion
//#region src/gateway/server-methods/system.ts
const systemHandlers = {
"gateway.identity.get": ({ respond }) => {
const identity = loadOrCreateDeviceIdentity();
respond(true, {
deviceId: identity.deviceId,
publicKey: publicKeyRawBase64UrlFromPem(identity.publicKeyPem)
}, void 0);
},
"last-heartbeat": ({ respond }) => {
respond(true, getLastHeartbeatEvent(), void 0);
},
"set-heartbeats": ({ params, respond }) => {
const enabled = params.enabled;
if (typeof enabled !== "boolean") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid set-heartbeats params: enabled (boolean) required"));
return;
}
setHeartbeatsEnabled(enabled);
respond(true, {
ok: true,
enabled
}, void 0);
},
"system-presence": ({ respond }) => {
respond(true, listSystemPresence(), void 0);
},
"system-event": ({ params, respond, context }) => {
const text = typeof params.text === "string" ? params.text.trim() : "";
if (!text) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "text required"));
return;
}
const sessionKey = resolveMainSessionKeyFromConfig();
const deviceId = typeof params.deviceId === "string" ? params.deviceId : void 0;
const instanceId = typeof params.instanceId === "string" ? params.instanceId : void 0;
const host = typeof params.host === "string" ? params.host : void 0;
const ip = typeof params.ip === "string" ? params.ip : void 0;
const mode = typeof params.mode === "string" ? params.mode : void 0;
const version = typeof params.version === "string" ? params.version : void 0;
const platform = typeof params.platform === "string" ? params.platform : void 0;
const deviceFamily = typeof params.deviceFamily === "string" ? params.deviceFamily : void 0;
const modelIdentifier = typeof params.modelIdentifier === "string" ? params.modelIdentifier : void 0;
const lastInputSeconds = typeof params.lastInputSeconds === "number" && Number.isFinite(params.lastInputSeconds) ? params.lastInputSeconds : void 0;
const reason = typeof params.reason === "string" ? params.reason : void 0;
const presenceUpdate = updateSystemPresence({
text,
deviceId,
instanceId,
host,
ip,
mode,
version,
platform,
deviceFamily,
modelIdentifier,
lastInputSeconds,
reason,
roles: Array.isArray(params.roles) && params.roles.every((t) => typeof t === "string") ? params.roles : void 0,
scopes: Array.isArray(params.scopes) && params.scopes.every((t) => typeof t === "string") ? params.scopes : void 0,
tags: Array.isArray(params.tags) && params.tags.every((t) => typeof t === "string") ? params.tags : void 0
});
if (text.startsWith("Node:")) {
const next = presenceUpdate.next;
const changed = new Set(presenceUpdate.changedKeys);
const reasonValue = next.reason ?? reason;
const normalizedReason = (reasonValue ?? "").toLowerCase();
const ignoreReason = normalizedReason.startsWith("periodic") || normalizedReason === "heartbeat";
const hostChanged = changed.has("host");
const ipChanged = changed.has("ip");
const versionChanged = changed.has("version");
const modeChanged = changed.has("mode");
const reasonChanged = changed.has("reason") && !ignoreReason;
if (hostChanged || ipChanged || versionChanged || modeChanged || reasonChanged) {
const contextChanged = isSystemEventContextChanged(sessionKey, presenceUpdate.key);
const parts = [];
if (contextChanged || hostChanged || ipChanged) {
const hostLabel = next.host?.trim() || "Unknown";
const ipLabel = next.ip?.trim();
parts.push(`Node: ${hostLabel}${ipLabel ? ` (${ipLabel})` : ""}`);
}
if (versionChanged) parts.push(`app ${next.version?.trim() || "unknown"}`);
if (modeChanged) parts.push(`mode ${next.mode?.trim() || "unknown"}`);
if (reasonChanged) parts.push(`reason ${reasonValue?.trim() || "event"}`);
const deltaText = parts.join(" · ");
if (deltaText) enqueueSystemEvent(deltaText, {
sessionKey,
contextKey: presenceUpdate.key
});
}
} else enqueueSystemEvent(text, { sessionKey });
broadcastPresenceSnapshot({
broadcast: context.broadcast,
incrementPresenceVersion: context.incrementPresenceVersion,
getHealthVersion: context.getHealthVersion
});
respond(true, { ok: true }, void 0);
}
};
//#endregion
//#region src/gateway/server-methods/talk.ts
const ADMIN_SCOPE = "operator.admin";
const TALK_SECRETS_SCOPE = "operator.talk.secrets";
function canReadTalkSecrets(client) {
const scopes = Array.isArray(client?.connect?.scopes) ? client.connect.scopes : [];
return scopes.includes(ADMIN_SCOPE) || scopes.includes(TALK_SECRETS_SCOPE);
}
function trimString(value) {
if (typeof value !== "string") return;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : void 0;
}
function finiteNumber(value) {
return typeof value === "number" && Number.isFinite(value) ? value : void 0;
}
function optionalBoolean(value) {
return typeof value === "boolean" ? value : void 0;
}
function plainObject(value) {
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
}
function normalizeTextNormalization(value) {
const normalized = trimString(value)?.toLowerCase();
return normalized === "auto" || normalized === "on" || normalized === "off" ? normalized : void 0;
}
function normalizeAliasKey(value) {
return value.trim().toLowerCase();
}
function resolveTalkVoiceId(providerConfig, requested) {
if (!requested) return;
const aliases = providerConfig.voiceAliases;
if (!aliases) return requested;
const normalizedRequested = normalizeAliasKey(requested);
for (const [alias, voiceId] of Object.entries(aliases)) if (normalizeAliasKey(alias) === normalizedRequested) return voiceId;
return requested;
}
function readTalkVoiceSettings(providerConfig) {
const source = plainObject(providerConfig.voiceSettings);
if (!source) return;
const stability = finiteNumber(source.stability);
const similarityBoost = finiteNumber(source.similarityBoost);
const style = finiteNumber(source.style);
const useSpeakerBoost = optionalBoolean(source.useSpeakerBoost);
const speed = finiteNumber(source.speed);
const voiceSettings = {
...stability == null ? {} : { stability },
...similarityBoost == null ? {} : { similarityBoost },
...style == null ? {} : { style },
...useSpeakerBoost == null ? {} : { useSpeakerBoost },
...speed == null ? {} : { speed }
};
return Object.keys(voiceSettings).length > 0 ? voiceSettings : void 0;
}
function buildTalkTtsConfig(config) {
const resolved = resolveActiveTalkProviderConfig(config.talk);
const provider = normalizeSpeechProviderId(resolved?.provider);
if (!resolved || !provider) return { error: "talk.speak unavailable: talk provider not configured" };
const baseTts = config.messages?.tts ?? {};
const providerConfig = resolved.config;
const talkTts = {
...baseTts,
auto: "always",
provider
};
const baseUrl = trimString(providerConfig.baseUrl);
const voiceId = trimString(providerConfig.voiceId);
const modelId = trimString(providerConfig.modelId);
const languageCode = trimString(providerConfig.languageCode);
if (provider === "elevenlabs") {
const seed = finiteNumber(providerConfig.seed);
const applyTextNormalization = normalizeTextNormalization(providerConfig.applyTextNormalization);
const voiceSettings = readTalkVoiceSettings(providerConfig);
talkTts.elevenlabs = {
...baseTts.elevenlabs,
...providerConfig.apiKey === void 0 ? {} : { apiKey: providerConfig.apiKey },
...baseUrl == null ? {} : { baseUrl },
...voiceId == null ? {} : { voiceId },
...modelId == null ? {} : { modelId },
...seed == null ? {} : { seed },
...applyTextNormalization == null ? {} : { applyTextNormalization },
...languageCode == null ? {} : { languageCode },
...voiceSettings == null ? {} : { voiceSettings }
};
} else if (provider === "openai") {
const speed = finiteNumber(providerConfig.speed);
const instructions = trimString(providerConfig.instructions);
talkTts.openai = {
...baseTts.openai,
...providerConfig.apiKey === void 0 ? {} : { apiKey: providerConfig.apiKey },
...baseUrl == null ? {} : { baseUrl },
...modelId == null ? {} : { model: modelId },
...voiceId == null ? {} : { voice: voiceId },
...speed == null ? {} : { speed },
...instructions == null ? {} : { instructions }
};
} else if (provider === "microsoft") {
const outputFormat = trimString(providerConfig.outputFormat);
const pitch = trimString(providerConfig.pitch);
const rate = trimString(providerConfig.rate);
const volume = trimString(providerConfig.volume);
const proxy = trimString(providerConfig.proxy);
const timeoutMs = finiteNumber(providerConfig.timeoutMs);
talkTts.microsoft = {
...baseTts.microsoft,
enabled: true,
...voiceId == null ? {} : { voice: voiceId },
...languageCode == null ? {} : { lang: languageCode },
...outputFormat == null ? {} : { outputFormat },
...pitch == null ? {} : { pitch },
...rate == null ? {} : { rate },
...volume == null ? {} : { volume },
...proxy == null ? {} : { proxy },
...timeoutMs == null ? {} : { timeoutMs }
};
}
return {
provider,
providerConfig,
cfg: {
...config,
messages: {
...config.messages,
tts: talkTts
}
}
};
}
function buildTalkSpeakOverrides(provider, providerConfig, params) {
const voiceId = resolveTalkVoiceId(providerConfig, trimString(params.voiceId));
const modelId = trimString(params.modelId);
const outputFormat = trimString(params.outputFormat);
const speed = finiteNumber(params.speed);
const seed = finiteNumber(params.seed);
const normalize = normalizeTextNormalization(params.normalize);
const language = trimString(params.language)?.toLowerCase();
const overrides = { provider };
if (provider === "elevenlabs") {
const voiceSettings = {
...speed == null ? {} : { speed },
...finiteNumber(params.stability) == null ? {} : { stability: finiteNumber(params.stability) },
...finiteNumber(params.similarity) == null ? {} : { similarityBoost: finiteNumber(params.similarity) },
...finiteNumber(params.style) == null ? {} : { style: finiteNumber(params.style) },
...optionalBoolean(params.speakerBoost) == null ? {} : { useSpeakerBoost: optionalBoolean(params.speakerBoost) }
};
overrides.elevenlabs = {
...voiceId == null ? {} : { voiceId },
...modelId == null ? {} : { modelId },
...outputFormat == null ? {} : { outputFormat },
...seed == null ? {} : { seed },
...normalize == null ? {} : { applyTextNormalization: normalize },
...language == null ? {} : { languageCode: language },
...Object.keys(voiceSettings).length === 0 ? {} : { voiceSettings }
};
return overrides;
}
if (provider === "openai") {
overrides.openai = {
...voiceId == null ? {} : { voice: voiceId },
...modelId == null ? {} : { model: modelId },
...speed == null ? {} : { speed }
};
return overrides;
}
if (provider === "microsoft") overrides.microsoft = {
...voiceId == null ? {} : { voice: voiceId },
...outputFormat == null ? {} : { outputFormat }
};
return overrides;
}
function inferMimeType(outputFormat, fileExtension) {
const normalizedOutput = outputFormat?.trim().toLowerCase();
const normalizedExtension = fileExtension?.trim().toLowerCase();
if (normalizedOutput === "mp3" || normalizedOutput?.startsWith("mp3_") || normalizedOutput?.endsWith("-mp3") || normalizedExtension === ".mp3") return "audio/mpeg";
if (normalizedOutput === "opus" || normalizedOutput?.startsWith("opus_") || normalizedExtension === ".opus" || normalizedExtension === ".ogg") return "audio/ogg";
if (normalizedOutput?.endsWith("-wav") || normalizedExtension === ".wav") return "audio/wav";
if (normalizedOutput?.endsWith("-webm") || normalizedExtension === ".webm") return "audio/webm";
}
const talkHandlers = {
"talk.config": async ({ params, respond, client }) => {
if (!validateTalkConfigParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid talk.config params: ${formatValidationErrors(validateTalkConfigParams.errors)}`));
return;
}
const includeSecrets = Boolean(params.includeSecrets);
if (includeSecrets && !canReadTalkSecrets(client)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `missing scope: ${TALK_SECRETS_SCOPE}`));
return;
}
const snapshot = await readConfigFileSnapshot();
const configPayload = {};
const talk = buildTalkConfigResponse(includeSecrets ? snapshot.config.talk : redactConfigObject(snapshot.config.talk));
if (talk) configPayload.talk = talk;
const sessionMainKey = snapshot.config.session?.mainKey;
if (typeof sessionMainKey === "string") configPayload.session = { mainKey: sessionMainKey };
const seamColor = snapshot.config.ui?.seamColor;
if (typeof seamColor === "string") configPayload.ui = { seamColor };
respond(true, { config: configPayload }, void 0);
},
"talk.speak": async ({ params, respond }) => {
if (!validateTalkSpeakParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid talk.speak params: ${formatValidationErrors(validateTalkSpeakParams.errors)}`));
return;
}
const text = trimString(params.text);
if (!text) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "talk.speak requires text"));
return;
}
try {
const setup = buildTalkTtsConfig((await readConfigFileSnapshot()).config);
if ("error" in setup) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, setup.error));
return;
}
const overrides = buildTalkSpeakOverrides(setup.provider, setup.providerConfig, params);
const result = await synthesizeSpeech({
text,
cfg: setup.cfg,
overrides,
disableFallback: true
});
if (!result.success || !result.audioBuffer) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, result.error ?? "talk synthesis failed"));
return;
}
respond(true, {
audioBase64: result.audioBuffer.toString("base64"),
provider: result.provider ?? setup.provider,
outputFormat: result.outputFormat,
voiceCompatible: result.voiceCompatible,
mimeType: inferMimeType(result.outputFormat, result.fileExtension),
fileExtension: result.fileExtension
}, void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"talk.mode": ({ params, respond, context, client, isWebchatConnect }) => {
if (client && isWebchatConnect(client.connect) && !context.hasConnectedMobileNode()) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "talk disabled: no connected iOS/Android nodes"));
return;
}
if (!validateTalkModeParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid talk.mode params: ${formatValidationErrors(validateTalkModeParams.errors)}`));
return;
}
const payload = {
enabled: params.enabled,
phase: params.phase ?? null,
ts: Date.now()
};
context.broadcast("talk.mode", payload, { dropIfSlow: true });
respond(true, payload, void 0);
}
};
//#endregion
//#region src/gateway/server-methods/tools-catalog.ts
function resolveAgentIdOrRespondError(rawAgentId, respond) {
const cfg = loadConfig();
const knownAgents = listAgentIds(cfg);
const requestedAgentId = typeof rawAgentId === "string" ? rawAgentId.trim() : "";
const agentId = requestedAgentId || resolveDefaultAgentId(cfg);
if (requestedAgentId && !knownAgents.includes(agentId)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unknown agent id "${requestedAgentId}"`));
return null;
}
return {
cfg,
agentId
};
}
function buildCoreGroups() {
return listCoreToolSections().map((section) => ({
id: section.id,
label: section.label,
source: "core",
tools: section.tools.map((tool) => ({
id: tool.id,
label: tool.label,
description: tool.description,
source: "core",
defaultProfiles: resolveCoreToolProfiles(tool.id)
}))
}));
}
function buildPluginGroups(params) {
const workspaceDir = resolveAgentWorkspaceDir(params.cfg, params.agentId);
const agentDir = resolveAgentDir(params.cfg, params.agentId);
const pluginTools = resolvePluginTools({
context: {
config: params.cfg,
workspaceDir,
agentDir,
agentId: params.agentId
},
existingToolNames: params.existingToolNames,
toolAllowlist: ["group:plugins"],
suppressNameConflicts: true,
allowGatewaySubagentBinding: true
});
const groups = /* @__PURE__ */ new Map();
for (const tool of pluginTools) {
const meta = getPluginToolMeta(tool);
const pluginId = meta?.pluginId ?? "plugin";
const groupId = `plugin:${pluginId}`;
const existing = groups.get(groupId) ?? {
id: groupId,
label: pluginId,
source: "plugin",
pluginId,
tools: []
};
existing.tools.push({
id: tool.name,
label: typeof tool.label === "string" && tool.label.trim() ? tool.label.trim() : tool.name,
description: typeof tool.description === "string" && tool.description.trim() ? tool.description.trim() : "Plugin tool",
source: "plugin",
pluginId,
optional: meta?.optional,
defaultProfiles: []
});
groups.set(groupId, existing);
}
return [...groups.values()].map((group) => ({
...group,
tools: group.tools.toSorted((a, b) => a.id.localeCompare(b.id))
})).toSorted((a, b) => a.label.localeCompare(b.label));
}
const toolsCatalogHandlers = { "tools.catalog": ({ params, respond }) => {
if (!validateToolsCatalogParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid tools.catalog params: ${formatValidationErrors(validateToolsCatalogParams.errors)}`));
return;
}
const resolved = resolveAgentIdOrRespondError(params.agentId, respond);
if (!resolved) return;
const includePlugins = params.includePlugins !== false;
const groups = buildCoreGroups();
if (includePlugins) {
const existingToolNames = new Set(groups.flatMap((group) => group.tools.map((tool) => tool.id)));
groups.push(...buildPluginGroups({
cfg: resolved.cfg,
agentId: resolved.agentId,
existingToolNames
}));
}
respond(true, {
agentId: resolved.agentId,
profiles: PROFILE_OPTIONS.map((profile) => ({
id: profile.id,
label: profile.label
})),
groups
}, void 0);
} };
//#endregion
//#region src/gateway/server-methods/tts.ts
const ttsHandlers = {
"tts.status": async ({ respond }) => {
try {
const cfg = loadConfig();
const config = resolveTtsConfig(cfg);
const prefsPath = resolveTtsPrefsPath(config);
const provider = getTtsProvider(config, prefsPath);
const autoMode = resolveTtsAutoMode({
config,
prefsPath
});
const fallbackProviders = resolveTtsProviderOrder(provider, cfg).slice(1).filter((candidate) => isTtsProviderConfigured(config, candidate, cfg));
respond(true, {
enabled: isTtsEnabled(config, prefsPath),
auto: autoMode,
provider,
fallbackProvider: fallbackProviders[0] ?? null,
fallbackProviders,
prefsPath,
hasOpenAIKey: Boolean(resolveTtsApiKey(config, "openai")),
hasElevenLabsKey: Boolean(resolveTtsApiKey(config, "elevenlabs")),
microsoftEnabled: isTtsProviderConfigured(config, "microsoft", cfg)
});
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"tts.enable": async ({ respond }) => {
try {
setTtsEnabled(resolveTtsPrefsPath(resolveTtsConfig(loadConfig())), true);
respond(true, { enabled: true });
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"tts.disable": async ({ respond }) => {
try {
setTtsEnabled(resolveTtsPrefsPath(resolveTtsConfig(loadConfig())), false);
respond(true, { enabled: false });
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"tts.convert": async ({ params, respond }) => {
const text = typeof params.text === "string" ? params.text.trim() : "";
if (!text) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "tts.convert requires text"));
return;
}
try {
const result = await textToSpeech({
text,
cfg: loadConfig(),
channel: typeof params.channel === "string" ? params.channel.trim() : void 0
});
if (result.success && result.audioPath) {
respond(true, {
audioPath: result.audioPath,
provider: result.provider,
outputFormat: result.outputFormat,
voiceCompatible: result.voiceCompatible
});
return;
}
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, result.error ?? "TTS conversion failed"));
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"tts.setProvider": async ({ params, respond }) => {
const provider = normalizeSpeechProviderId(typeof params.provider === "string" ? params.provider.trim() : "");
const cfg = loadConfig();
if (!provider || !getSpeechProvider(provider, cfg)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "Invalid provider. Use a registered TTS provider id such as openai, elevenlabs, or microsoft."));
return;
}
try {
setTtsProvider(resolveTtsPrefsPath(resolveTtsConfig(cfg)), provider);
respond(true, { provider });
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"tts.providers": async ({ respond }) => {
try {
const cfg = loadConfig();
const config = resolveTtsConfig(cfg);
const prefsPath = resolveTtsPrefsPath(config);
respond(true, {
providers: listSpeechProviders(cfg).map((provider) => ({
id: provider.id,
name: provider.label,
configured: provider.isConfigured({
cfg,
config
}),
models: provider.id === "openai" && provider.models == null ? [...OPENAI_TTS_MODELS] : [...provider.models ?? []],
voices: provider.id === "openai" && provider.voices == null ? [...OPENAI_TTS_VOICES] : [...provider.voices ?? []]
})),
active: getTtsProvider(config, prefsPath)
});
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
}
};
//#endregion
//#region src/gateway/server-methods/update.ts
const updateHandlers = { "update.run": async ({ params, respond, client, context }) => {
if (!assertValidParams(params, validateUpdateRunParams, "update.run", respond)) return;
const actor = resolveControlPlaneActor(client);
const { sessionKey, note, restartDelayMs } = parseRestartRequestParams(params);
const { deliveryContext, threadId } = extractDeliveryInfo(sessionKey);
const timeoutMsRaw = params.timeoutMs;
const timeoutMs = typeof timeoutMsRaw === "number" && Number.isFinite(timeoutMsRaw) ? Math.max(1e3, Math.floor(timeoutMsRaw)) : void 0;
let result;
try {
const configChannel = normalizeUpdateChannel(loadConfig().update?.channel);
result = await runGatewayUpdate({
timeoutMs,
cwd: await resolveOpenClawPackageRoot({
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
}) ?? process.cwd(),
argv1: process.argv[1],
channel: configChannel ?? void 0
});
} catch (err) {
result = {
status: "error",
mode: "unknown",
reason: String(err),
steps: [],
durationMs: 0
};
}
const payload = {
kind: "update",
status: result.status,
ts: Date.now(),
sessionKey,
deliveryContext,
threadId,
message: note ?? null,
doctorHint: formatDoctorNonInteractiveHint(),
stats: {
mode: result.mode,
root: result.root ?? void 0,
before: result.before ?? null,
after: result.after ?? null,
steps: result.steps.map((step) => ({
name: step.name,
command: step.command,
cwd: step.cwd,
durationMs: step.durationMs,
log: {
stdoutTail: step.stdoutTail ?? null,
stderrTail: step.stderrTail ?? null,
exitCode: step.exitCode ?? null
}
})),
reason: result.reason ?? null,
durationMs: result.durationMs
}
};
let sentinelPath = null;
try {
sentinelPath = await writeRestartSentinel(payload);
} catch {
sentinelPath = null;
}
const restart = result.status === "ok" ? scheduleGatewaySigusr1Restart({
delayMs: restartDelayMs,
reason: "update.run",
audit: {
actor: actor.actor,
deviceId: actor.deviceId,
clientIp: actor.clientIp,
changedPaths: []
}
}) : null;
context?.logGateway?.info(`update.run completed ${formatControlPlaneActor(actor)} changedPaths=<n/a> restartReason=update.run status=${result.status}`);
if (restart?.coalesced) context?.logGateway?.warn(`update.run restart coalesced ${formatControlPlaneActor(actor)} delayMs=${restart.delayMs}`);
respond(true, {
ok: result.status !== "error",
result,
restart,
sentinel: {
path: sentinelPath,
payload
}
}, void 0);
} };
//#endregion
//#region src/shared/usage-aggregates.ts
function mergeUsageLatency(totals, latency) {
if (!latency || latency.count <= 0) return;
totals.count += latency.count;
totals.sum += latency.avgMs * latency.count;
totals.min = Math.min(totals.min, latency.minMs);
totals.max = Math.max(totals.max, latency.maxMs);
totals.p95Max = Math.max(totals.p95Max, latency.p95Ms);
}
function mergeUsageDailyLatency(dailyLatencyMap, dailyLatency) {
for (const day of dailyLatency ?? []) {
const existing = dailyLatencyMap.get(day.date) ?? {
date: day.date,
count: 0,
sum: 0,
min: Number.POSITIVE_INFINITY,
max: 0,
p95Max: 0
};
existing.count += day.count;
existing.sum += day.avgMs * day.count;
existing.min = Math.min(existing.min, day.minMs);
existing.max = Math.max(existing.max, day.maxMs);
existing.p95Max = Math.max(existing.p95Max, day.p95Ms);
dailyLatencyMap.set(day.date, existing);
}
}
function buildUsageAggregateTail(params) {
return {
byChannel: Array.from(params.byChannelMap.entries()).map(([channel, totals]) => ({
channel,
totals
})).toSorted((a, b) => b.totals.totalCost - a.totals.totalCost),
latency: params.latencyTotals.count > 0 ? {
count: params.latencyTotals.count,
avgMs: params.latencyTotals.sum / params.latencyTotals.count,
minMs: params.latencyTotals.min === Number.POSITIVE_INFINITY ? 0 : params.latencyTotals.min,
maxMs: params.latencyTotals.max,
p95Ms: params.latencyTotals.p95Max
} : void 0,
dailyLatency: Array.from(params.dailyLatencyMap.values()).map((entry) => ({
date: entry.date,
count: entry.count,
avgMs: entry.count ? entry.sum / entry.count : 0,
minMs: entry.min === Number.POSITIVE_INFINITY ? 0 : entry.min,
maxMs: entry.max,
p95Ms: entry.p95Max
})).toSorted((a, b) => a.date.localeCompare(b.date)),
modelDaily: Array.from(params.modelDailyMap.values()).toSorted((a, b) => a.date.localeCompare(b.date) || b.cost - a.cost),
daily: Array.from(params.dailyMap.values()).toSorted((a, b) => a.date.localeCompare(b.date))
};
}
//#endregion
//#region src/gateway/server-methods/usage.ts
const COST_USAGE_CACHE_TTL_MS = 3e4;
const DAY_MS = 1440 * 60 * 1e3;
const costUsageCache = /* @__PURE__ */ new Map();
function resolveSessionUsageFileOrRespond(key, respond) {
const config = loadConfig();
const { entry, storePath } = loadSessionEntry(key);
const parsed = parseAgentSessionKey(key);
const agentId = parsed?.agentId;
const rawSessionId = parsed?.rest ?? key;
const sessionId = entry?.sessionId ?? rawSessionId;
let sessionFile;
try {
sessionFile = resolveSessionFilePath(sessionId, entry, resolveSessionFilePathOptions({
storePath,
agentId
}));
} catch {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `Invalid session key: ${key}`));
return null;
}
return {
config,
entry,
agentId,
sessionId,
sessionFile
};
}
const parseDateParts = (raw) => {
if (typeof raw !== "string" || !raw.trim()) return;
const match = /^(\d{4})-(\d{2})-(\d{2})$/.exec(raw.trim());
if (!match) return;
const [, yearStr, monthStr, dayStr] = match;
const year = Number(yearStr);
const monthIndex = Number(monthStr) - 1;
const day = Number(dayStr);
if (!Number.isFinite(year) || !Number.isFinite(monthIndex) || !Number.isFinite(day)) return;
return {
year,
monthIndex,
day
};
};
/**
* Parse a UTC offset string in the format UTC+H, UTC-H, UTC+HH, UTC-HH, UTC+H:MM, UTC-HH:MM.
* Returns the UTC offset in minutes (east-positive), or undefined if invalid.
*/
const parseUtcOffsetToMinutes = (raw) => {
if (typeof raw !== "string" || !raw.trim()) return;
const match = /^UTC([+-])(\d{1,2})(?::([0-5]\d))?$/.exec(raw.trim());
if (!match) return;
const sign = match[1] === "+" ? 1 : -1;
const hours = Number(match[2]);
const minutes = Number(match[3] ?? "0");
if (!Number.isInteger(hours) || !Number.isInteger(minutes)) return;
if (hours > 14 || hours === 14 && minutes !== 0) return;
const totalMinutes = sign * (hours * 60 + minutes);
if (totalMinutes < -720 || totalMinutes > 840) return;
return totalMinutes;
};
const resolveDateInterpretation = (params) => {
if (params.mode === "gateway") return { mode: "gateway" };
if (params.mode === "specific") {
const utcOffsetMinutes = parseUtcOffsetToMinutes(params.utcOffset);
if (utcOffsetMinutes !== void 0) return {
mode: "specific",
utcOffsetMinutes
};
}
return { mode: "utc" };
};
/**
* Parse a date string (YYYY-MM-DD) to start-of-day timestamp based on interpretation mode.
* Returns undefined if invalid.
*/
const parseDateToMs = (raw, interpretation = { mode: "utc" }) => {
const parts = parseDateParts(raw);
if (!parts) return;
const { year, monthIndex, day } = parts;
if (interpretation.mode === "gateway") {
const ms = new Date(year, monthIndex, day).getTime();
return Number.isNaN(ms) ? void 0 : ms;
}
if (interpretation.mode === "specific") {
const ms = Date.UTC(year, monthIndex, day) - interpretation.utcOffsetMinutes * 60 * 1e3;
return Number.isNaN(ms) ? void 0 : ms;
}
const ms = Date.UTC(year, monthIndex, day);
return Number.isNaN(ms) ? void 0 : ms;
};
const getTodayStartMs = (now, interpretation) => {
if (interpretation.mode === "gateway") return new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
if (interpretation.mode === "specific") {
const shifted = new Date(now.getTime() + interpretation.utcOffsetMinutes * 60 * 1e3);
return Date.UTC(shifted.getUTCFullYear(), shifted.getUTCMonth(), shifted.getUTCDate()) - interpretation.utcOffsetMinutes * 60 * 1e3;
}
return Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate());
};
const parseDays = (raw) => {
if (typeof raw === "number" && Number.isFinite(raw)) return Math.floor(raw);
if (typeof raw === "string" && raw.trim() !== "") {
const parsed = Number(raw);
if (Number.isFinite(parsed)) return Math.floor(parsed);
}
};
/**
* Get date range from params (startDate/endDate or days).
* Falls back to last 30 days if not provided.
*/
const parseDateRange = (params) => {
const now = /* @__PURE__ */ new Date();
const interpretation = resolveDateInterpretation(params);
const todayStartMs = getTodayStartMs(now, interpretation);
const todayEndMs = todayStartMs + DAY_MS - 1;
const startMs = parseDateToMs(params.startDate, interpretation);
const endMs = parseDateToMs(params.endDate, interpretation);
if (startMs !== void 0 && endMs !== void 0) return {
startMs,
endMs: endMs + DAY_MS - 1
};
const days = parseDays(params.days);
if (days !== void 0) return {
startMs: todayStartMs - (Math.max(1, days) - 1) * DAY_MS,
endMs: todayEndMs
};
return {
startMs: todayStartMs - 29 * DAY_MS,
endMs: todayEndMs
};
};
function buildStoreBySessionId(store) {
const storeBySessionId = /* @__PURE__ */ new Map();
for (const [key, entry] of Object.entries(store)) if (entry?.sessionId) storeBySessionId.set(entry.sessionId, {
key,
entry
});
return storeBySessionId;
}
async function discoverAllSessionsForUsage(params) {
const agents = listAgentsForGateway(params.config).agents;
return (await Promise.all(agents.map(async (agent) => {
return (await discoverAllSessions({
agentId: agent.id,
startMs: params.startMs,
endMs: params.endMs
})).map((session) => ({
...session,
agentId: agent.id
}));
}))).flat().toSorted((a, b) => b.mtime - a.mtime);
}
async function loadCostUsageSummaryCached(params) {
const cacheKey = `${params.startMs}-${params.endMs}`;
const now = Date.now();
const cached = costUsageCache.get(cacheKey);
if (cached?.summary && cached.updatedAt && now - cached.updatedAt < COST_USAGE_CACHE_TTL_MS) return cached.summary;
if (cached?.inFlight) {
if (cached.summary) return cached.summary;
return await cached.inFlight;
}
const entry = cached ?? {};
const inFlight = loadCostUsageSummary({
startMs: params.startMs,
endMs: params.endMs,
config: params.config
}).then((summary) => {
costUsageCache.set(cacheKey, {
summary,
updatedAt: Date.now()
});
return summary;
}).catch((err) => {
if (entry.summary) return entry.summary;
throw err;
}).finally(() => {
const current = costUsageCache.get(cacheKey);
if (current?.inFlight === inFlight) {
current.inFlight = void 0;
costUsageCache.set(cacheKey, current);
}
});
entry.inFlight = inFlight;
costUsageCache.set(cacheKey, entry);
if (entry.summary) return entry.summary;
return await inFlight;
}
const usageHandlers = {
"usage.status": async ({ respond }) => {
respond(true, await loadProviderUsageSummary(), void 0);
},
"usage.cost": async ({ respond, params }) => {
const config = loadConfig();
const { startMs, endMs } = parseDateRange({
startDate: params?.startDate,
endDate: params?.endDate,
days: params?.days,
mode: params?.mode,
utcOffset: params?.utcOffset
});
respond(true, await loadCostUsageSummaryCached({
startMs,
endMs,
config
}), void 0);
},
"sessions.usage": async ({ respond, params }) => {
if (!validateSessionsUsageParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid sessions.usage params: ${formatValidationErrors(validateSessionsUsageParams.errors)}`));
return;
}
const p = params;
const config = loadConfig();
const { startMs, endMs } = parseDateRange({
startDate: p.startDate,
endDate: p.endDate,
mode: p.mode,
utcOffset: p.utcOffset
});
const limit = typeof p.limit === "number" && Number.isFinite(p.limit) ? p.limit : 50;
const includeContextWeight = p.includeContextWeight ?? false;
const specificKey = typeof p.key === "string" ? p.key.trim() : null;
const { storePath, store } = loadCombinedSessionStoreForGateway(config);
const now = Date.now();
const mergedEntries = [];
if (specificKey) {
const parsed = parseAgentSessionKey(specificKey);
const agentIdFromKey = parsed?.agentId;
const keyRest = parsed?.rest ?? specificKey;
const storeBySessionId = buildStoreBySessionId(store);
const storeMatch = store[specificKey] ? {
key: specificKey,
entry: store[specificKey]
} : null;
const storeByIdMatch = storeBySessionId.get(keyRest) ?? null;
const resolvedStoreKey = storeMatch?.key ?? storeByIdMatch?.key ?? specificKey;
const storeEntry = storeMatch?.entry ?? storeByIdMatch?.entry;
const sessionId = storeEntry?.sessionId ?? keyRest;
let sessionFile;
try {
sessionFile = resolveExistingUsageSessionFile({
sessionId,
sessionEntry: storeEntry,
sessionFile: resolveSessionFilePath(sessionId, storeEntry, resolveSessionFilePathOptions({
storePath: storePath !== "(multiple)" ? storePath : void 0,
agentId: agentIdFromKey
})),
agentId: agentIdFromKey
});
} catch {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `Invalid session reference: ${specificKey}`));
return;
}
if (sessionFile) try {
const stats = fs.statSync(sessionFile);
if (stats.isFile()) mergedEntries.push({
key: resolvedStoreKey,
sessionId,
sessionFile,
label: storeEntry?.label,
updatedAt: storeEntry?.updatedAt ?? stats.mtimeMs,
storeEntry
});
} catch {}
} else {
const discoveredSessions = await discoverAllSessionsForUsage({
config,
startMs,
endMs
});
const storeBySessionId = buildStoreBySessionId(store);
for (const discovered of discoveredSessions) {
const storeMatch = storeBySessionId.get(discovered.sessionId);
if (storeMatch) mergedEntries.push({
key: storeMatch.key,
sessionId: discovered.sessionId,
sessionFile: discovered.sessionFile,
label: storeMatch.entry.label,
updatedAt: storeMatch.entry.updatedAt ?? discovered.mtime,
storeEntry: storeMatch.entry
});
else mergedEntries.push({
key: `agent:${discovered.agentId}:${discovered.sessionId}`,
sessionId: discovered.sessionId,
sessionFile: discovered.sessionFile,
label: void 0,
updatedAt: discovered.mtime
});
}
}
mergedEntries.sort((a, b) => b.updatedAt - a.updatedAt);
const limitedEntries = mergedEntries.slice(0, limit);
const sessions = [];
const aggregateTotals = {
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
totalCost: 0,
inputCost: 0,
outputCost: 0,
cacheReadCost: 0,
cacheWriteCost: 0,
missingCostEntries: 0
};
const aggregateMessages = {
total: 0,
user: 0,
assistant: 0,
toolCalls: 0,
toolResults: 0,
errors: 0
};
const toolAggregateMap = /* @__PURE__ */ new Map();
const byModelMap = /* @__PURE__ */ new Map();
const byProviderMap = /* @__PURE__ */ new Map();
const byAgentMap = /* @__PURE__ */ new Map();
const byChannelMap = /* @__PURE__ */ new Map();
const dailyAggregateMap = /* @__PURE__ */ new Map();
const latencyTotals = {
count: 0,
sum: 0,
min: Number.POSITIVE_INFINITY,
max: 0,
p95Max: 0
};
const dailyLatencyMap = /* @__PURE__ */ new Map();
const modelDailyMap = /* @__PURE__ */ new Map();
const emptyTotals = () => ({
input: 0,
output: 0,
cacheRead: 0,
cacheWrite: 0,
totalTokens: 0,
totalCost: 0,
inputCost: 0,
outputCost: 0,
cacheReadCost: 0,
cacheWriteCost: 0,
missingCostEntries: 0
});
const mergeTotals = (target, source) => {
target.input += source.input;
target.output += source.output;
target.cacheRead += source.cacheRead;
target.cacheWrite += source.cacheWrite;
target.totalTokens += source.totalTokens;
target.totalCost += source.totalCost;
target.inputCost += source.inputCost;
target.outputCost += source.outputCost;
target.cacheReadCost += source.cacheReadCost;
target.cacheWriteCost += source.cacheWriteCost;
target.missingCostEntries += source.missingCostEntries;
};
for (const merged of limitedEntries) {
const agentId = parseAgentSessionKey(merged.key)?.agentId;
const usage = await loadSessionCostSummary({
sessionId: merged.sessionId,
sessionEntry: merged.storeEntry,
sessionFile: merged.sessionFile,
config,
agentId,
startMs,
endMs
});
if (usage) {
aggregateTotals.input += usage.input;
aggregateTotals.output += usage.output;
aggregateTotals.cacheRead += usage.cacheRead;
aggregateTotals.cacheWrite += usage.cacheWrite;
aggregateTotals.totalTokens += usage.totalTokens;
aggregateTotals.totalCost += usage.totalCost;
aggregateTotals.inputCost += usage.inputCost;
aggregateTotals.outputCost += usage.outputCost;
aggregateTotals.cacheReadCost += usage.cacheReadCost;
aggregateTotals.cacheWriteCost += usage.cacheWriteCost;
aggregateTotals.missingCostEntries += usage.missingCostEntries;
}
const channel = merged.storeEntry?.channel ?? merged.storeEntry?.origin?.provider;
const chatType = merged.storeEntry?.chatType ?? merged.storeEntry?.origin?.chatType;
if (usage) {
if (usage.messageCounts) {
aggregateMessages.total += usage.messageCounts.total;
aggregateMessages.user += usage.messageCounts.user;
aggregateMessages.assistant += usage.messageCounts.assistant;
aggregateMessages.toolCalls += usage.messageCounts.toolCalls;
aggregateMessages.toolResults += usage.messageCounts.toolResults;
aggregateMessages.errors += usage.messageCounts.errors;
}
if (usage.toolUsage) for (const tool of usage.toolUsage.tools) toolAggregateMap.set(tool.name, (toolAggregateMap.get(tool.name) ?? 0) + tool.count);
if (usage.modelUsage) for (const entry of usage.modelUsage) {
const modelKey = `${entry.provider ?? "unknown"}::${entry.model ?? "unknown"}`;
const modelExisting = byModelMap.get(modelKey) ?? {
provider: entry.provider,
model: entry.model,
count: 0,
totals: emptyTotals()
};
modelExisting.count += entry.count;
mergeTotals(modelExisting.totals, entry.totals);
byModelMap.set(modelKey, modelExisting);
const providerKey = entry.provider ?? "unknown";
const providerExisting = byProviderMap.get(providerKey) ?? {
provider: entry.provider,
model: void 0,
count: 0,
totals: emptyTotals()
};
providerExisting.count += entry.count;
mergeTotals(providerExisting.totals, entry.totals);
byProviderMap.set(providerKey, providerExisting);
}
mergeUsageLatency(latencyTotals, usage.latency);
mergeUsageDailyLatency(dailyLatencyMap, usage.dailyLatency);
if (usage.dailyModelUsage) for (const entry of usage.dailyModelUsage) {
const key = `${entry.date}::${entry.provider ?? "unknown"}::${entry.model ?? "unknown"}`;
const existing = modelDailyMap.get(key) ?? {
date: entry.date,
provider: entry.provider,
model: entry.model,
tokens: 0,
cost: 0,
count: 0
};
existing.tokens += entry.tokens;
existing.cost += entry.cost;
existing.count += entry.count;
modelDailyMap.set(key, existing);
}
if (agentId) {
const agentTotals = byAgentMap.get(agentId) ?? emptyTotals();
mergeTotals(agentTotals, usage);
byAgentMap.set(agentId, agentTotals);
}
if (channel) {
const channelTotals = byChannelMap.get(channel) ?? emptyTotals();
mergeTotals(channelTotals, usage);
byChannelMap.set(channel, channelTotals);
}
if (usage.dailyBreakdown) for (const day of usage.dailyBreakdown) {
const daily = dailyAggregateMap.get(day.date) ?? {
date: day.date,
tokens: 0,
cost: 0,
messages: 0,
toolCalls: 0,
errors: 0
};
daily.tokens += day.tokens;
daily.cost += day.cost;
dailyAggregateMap.set(day.date, daily);
}
if (usage.dailyMessageCounts) for (const day of usage.dailyMessageCounts) {
const daily = dailyAggregateMap.get(day.date) ?? {
date: day.date,
tokens: 0,
cost: 0,
messages: 0,
toolCalls: 0,
errors: 0
};
daily.messages += day.total;
daily.toolCalls += day.toolCalls;
daily.errors += day.errors;
dailyAggregateMap.set(day.date, daily);
}
}
sessions.push({
key: merged.key,
label: merged.label,
sessionId: merged.sessionId,
updatedAt: merged.updatedAt,
agentId,
channel,
chatType,
origin: merged.storeEntry?.origin,
modelOverride: merged.storeEntry?.modelOverride,
providerOverride: merged.storeEntry?.providerOverride,
modelProvider: merged.storeEntry?.modelProvider,
model: merged.storeEntry?.model,
usage,
contextWeight: includeContextWeight ? merged.storeEntry?.systemPromptReport ?? null : void 0
});
}
const formatDateStr = (ms) => {
const d = new Date(ms);
return `${d.getUTCFullYear()}-${String(d.getUTCMonth() + 1).padStart(2, "0")}-${String(d.getUTCDate()).padStart(2, "0")}`;
};
const tail = buildUsageAggregateTail({
byChannelMap,
latencyTotals,
dailyLatencyMap,
modelDailyMap,
dailyMap: dailyAggregateMap
});
const aggregates = {
messages: aggregateMessages,
tools: {
totalCalls: Array.from(toolAggregateMap.values()).reduce((sum, count) => sum + count, 0),
uniqueTools: toolAggregateMap.size,
tools: Array.from(toolAggregateMap.entries()).map(([name, count]) => ({
name,
count
})).toSorted((a, b) => b.count - a.count)
},
byModel: Array.from(byModelMap.values()).toSorted((a, b) => {
const costDiff = (b.totals?.totalCost ?? 0) - (a.totals?.totalCost ?? 0);
if (costDiff !== 0) return costDiff;
return (b.totals?.totalTokens ?? 0) - (a.totals?.totalTokens ?? 0);
}),
byProvider: Array.from(byProviderMap.values()).toSorted((a, b) => {
const costDiff = (b.totals?.totalCost ?? 0) - (a.totals?.totalCost ?? 0);
if (costDiff !== 0) return costDiff;
return (b.totals?.totalTokens ?? 0) - (a.totals?.totalTokens ?? 0);
}),
byAgent: Array.from(byAgentMap.entries()).map(([id, totals]) => ({
agentId: id,
totals
})).toSorted((a, b) => (b.totals?.totalCost ?? 0) - (a.totals?.totalCost ?? 0)),
...tail
};
respond(true, {
updatedAt: now,
startDate: formatDateStr(startMs),
endDate: formatDateStr(endMs),
sessions,
totals: aggregateTotals,
aggregates
}, void 0);
},
"sessions.usage.timeseries": async ({ respond, params }) => {
const key = typeof params?.key === "string" ? params.key.trim() : null;
if (!key) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "key is required for timeseries"));
return;
}
const resolved = resolveSessionUsageFileOrRespond(key, respond);
if (!resolved) return;
const { config, entry, agentId, sessionId, sessionFile } = resolved;
const timeseries = await loadSessionUsageTimeSeries({
sessionId,
sessionEntry: entry,
sessionFile,
config,
agentId,
maxPoints: 200
});
if (!timeseries) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `No transcript found for session: ${key}`));
return;
}
respond(true, timeseries, void 0);
},
"sessions.usage.logs": async ({ respond, params }) => {
const key = typeof params?.key === "string" ? params.key.trim() : null;
if (!key) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "key is required for logs"));
return;
}
const limit = typeof params?.limit === "number" && Number.isFinite(params.limit) ? Math.min(params.limit, 1e3) : 200;
const resolved = resolveSessionUsageFileOrRespond(key, respond);
if (!resolved) return;
const { config, entry, agentId, sessionId, sessionFile } = resolved;
const { loadSessionLogs } = await import("./session-cost-usage-fxDbnAer.js");
respond(true, { logs: await loadSessionLogs({
sessionId,
sessionEntry: entry,
sessionFile,
config,
agentId,
limit
}) ?? [] }, void 0);
}
};
//#endregion
//#region src/gateway/server-methods/voicewake.ts
const voicewakeHandlers = {
"voicewake.get": async ({ respond }) => {
try {
respond(true, { triggers: (await loadVoiceWakeConfig()).triggers });
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"voicewake.set": async ({ params, respond, context }) => {
if (!Array.isArray(params.triggers)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "voicewake.set requires triggers: string[]"));
return;
}
try {
const cfg = await setVoiceWakeTriggers(normalizeVoiceWakeTriggers(params.triggers));
context.broadcastVoiceWakeChanged(cfg.triggers);
respond(true, { triggers: cfg.triggers });
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
}
};
//#endregion
//#region src/gateway/server-methods/web.ts
const WEB_LOGIN_METHODS = new Set(["web.login.start", "web.login.wait"]);
const resolveWebLoginProvider = () => listChannelPlugins().find((plugin) => (plugin.gatewayMethods ?? []).some((method) => WEB_LOGIN_METHODS.has(method))) ?? null;
function resolveAccountId(params) {
return typeof params.accountId === "string" ? params.accountId : void 0;
}
function respondProviderUnavailable(respond) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "web login provider is not available"));
}
function respondProviderUnsupported(respond, providerId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `web login is not supported by provider ${providerId}`));
}
const webHandlers = {
"web.login.start": async ({ params, respond, context }) => {
if (!validateWebLoginStartParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid web.login.start params: ${formatValidationErrors(validateWebLoginStartParams.errors)}`));
return;
}
try {
const accountId = resolveAccountId(params);
const provider = resolveWebLoginProvider();
if (!provider) {
respondProviderUnavailable(respond);
return;
}
await context.stopChannel(provider.id, accountId);
if (!provider.gateway?.loginWithQrStart) {
respondProviderUnsupported(respond, provider.id);
return;
}
respond(true, await provider.gateway.loginWithQrStart({
force: Boolean(params.force),
timeoutMs: typeof params.timeoutMs === "number" ? params.timeoutMs : void 0,
verbose: Boolean(params.verbose),
accountId
}), void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
},
"web.login.wait": async ({ params, respond, context }) => {
if (!validateWebLoginWaitParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid web.login.wait params: ${formatValidationErrors(validateWebLoginWaitParams.errors)}`));
return;
}
try {
const accountId = resolveAccountId(params);
const provider = resolveWebLoginProvider();
if (!provider) {
respondProviderUnavailable(respond);
return;
}
if (!provider.gateway?.loginWithQrWait) {
respondProviderUnsupported(respond, provider.id);
return;
}
const result = await provider.gateway.loginWithQrWait({
timeoutMs: typeof params.timeoutMs === "number" ? params.timeoutMs : void 0,
accountId
});
if (result.connected) await context.startChannel(provider.id, accountId);
respond(true, result, void 0);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
}
}
};
//#endregion
//#region src/wizard/session.ts
function createDeferred() {
let resolve;
let reject;
return {
promise: new Promise((res, rej) => {
resolve = res;
reject = rej;
}),
resolve,
reject
};
}
var WizardSessionPrompter = class {
constructor(session) {
this.session = session;
}
async intro(title) {
await this.prompt({
type: "note",
title,
message: "",
executor: "client"
});
}
async outro(message) {
await this.prompt({
type: "note",
title: "Done",
message,
executor: "client"
});
}
async note(message, title) {
await this.prompt({
type: "note",
title,
message,
executor: "client"
});
}
async select(params) {
return await this.prompt({
type: "select",
message: params.message,
options: params.options.map((opt) => ({
value: opt.value,
label: opt.label,
hint: opt.hint
})),
initialValue: params.initialValue,
executor: "client"
});
}
async multiselect(params) {
const res = await this.prompt({
type: "multiselect",
message: params.message,
options: params.options.map((opt) => ({
value: opt.value,
label: opt.label,
hint: opt.hint
})),
initialValue: params.initialValues,
executor: "client"
});
return Array.isArray(res) ? res : [];
}
async text(params) {
const res = await this.prompt({
type: "text",
message: params.message,
initialValue: params.initialValue,
placeholder: params.placeholder,
executor: "client"
});
const value = res === null || res === void 0 ? "" : typeof res === "string" ? res : typeof res === "number" || typeof res === "boolean" || typeof res === "bigint" ? String(res) : "";
const error = params.validate?.(value);
if (error) throw new Error(error);
return value;
}
async confirm(params) {
const res = await this.prompt({
type: "confirm",
message: params.message,
initialValue: params.initialValue,
executor: "client"
});
return Boolean(res);
}
progress(_label) {
return {
update: (_message) => {},
stop: (_message) => {}
};
}
async prompt(step) {
return await this.session.awaitAnswer({
...step,
id: randomUUID()
});
}
};
var WizardSession = class {
constructor(runner) {
this.runner = runner;
this.currentStep = null;
this.stepDeferred = null;
this.answerDeferred = /* @__PURE__ */ new Map();
this.status = "running";
const prompter = new WizardSessionPrompter(this);
this.run(prompter);
}
async next() {
if (this.currentStep) return {
done: false,
step: this.currentStep,
status: this.status
};
if (this.status !== "running") return {
done: true,
status: this.status,
error: this.error
};
if (!this.stepDeferred) this.stepDeferred = createDeferred();
const step = await this.stepDeferred.promise;
if (step) return {
done: false,
step,
status: this.status
};
return {
done: true,
status: this.status,
error: this.error
};
}
async answer(stepId, value) {
const deferred = this.answerDeferred.get(stepId);
if (!deferred) throw new Error("wizard: no pending step");
this.answerDeferred.delete(stepId);
this.currentStep = null;
deferred.resolve(value);
}
cancel() {
if (this.status !== "running") return;
this.status = "cancelled";
this.error = "cancelled";
this.currentStep = null;
for (const [, deferred] of this.answerDeferred) deferred.reject(new WizardCancelledError());
this.answerDeferred.clear();
this.resolveStep(null);
}
pushStep(step) {
this.currentStep = step;
this.resolveStep(step);
}
async run(prompter) {
try {
await this.runner(prompter);
this.status = "done";
} catch (err) {
if (err instanceof WizardCancelledError) {
this.status = "cancelled";
this.error = err.message;
} else {
this.status = "error";
this.error = String(err);
}
} finally {
this.resolveStep(null);
}
}
async awaitAnswer(step) {
if (this.status !== "running") throw new Error("wizard: session not running");
this.pushStep(step);
const deferred = createDeferred();
this.answerDeferred.set(step.id, deferred);
return await deferred.promise;
}
resolveStep(step) {
if (!this.stepDeferred) return;
const deferred = this.stepDeferred;
this.stepDeferred = null;
deferred.resolve(step);
}
getStatus() {
return this.status;
}
getError() {
return this.error;
}
};
//#endregion
//#region src/gateway/server-methods/wizard.ts
function readWizardStatus(session) {
return {
status: session.getStatus(),
error: session.getError()
};
}
function findWizardSessionOrRespond(params) {
const session = params.context.wizardSessions.get(params.sessionId);
if (!session) {
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not found"));
return null;
}
return session;
}
const wizardHandlers = {
"wizard.start": async ({ params, respond, context }) => {
if (!assertValidParams(params, validateWizardStartParams, "wizard.start", respond)) return;
if (context.findRunningWizard()) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "wizard already running"));
return;
}
const sessionId = randomUUID();
const opts = {
mode: params.mode,
workspace: typeof params.workspace === "string" ? params.workspace : void 0
};
const session = new WizardSession((prompter) => context.wizardRunner(opts, defaultRuntime, prompter));
context.wizardSessions.set(sessionId, session);
const result = await session.next();
if (result.done) context.purgeWizardSession(sessionId);
respond(true, {
sessionId,
...result
}, void 0);
},
"wizard.next": async ({ params, respond, context }) => {
if (!assertValidParams(params, validateWizardNextParams, "wizard.next", respond)) return;
const sessionId = params.sessionId;
const session = findWizardSessionOrRespond({
context,
respond,
sessionId
});
if (!session) return;
const answer = params.answer;
if (answer) {
if (session.getStatus() !== "running") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "wizard not running"));
return;
}
try {
await session.answer(String(answer.stepId ?? ""), answer.value);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, formatForLog(err)));
return;
}
}
const result = await session.next();
if (result.done) context.purgeWizardSession(sessionId);
respond(true, result, void 0);
},
"wizard.cancel": ({ params, respond, context }) => {
if (!assertValidParams(params, validateWizardCancelParams, "wizard.cancel", respond)) return;
const sessionId = params.sessionId;
const session = findWizardSessionOrRespond({
context,
respond,
sessionId
});
if (!session) return;
session.cancel();
const status = readWizardStatus(session);
context.wizardSessions.delete(sessionId);
respond(true, status, void 0);
},
"wizard.status": ({ params, respond, context }) => {
if (!assertValidParams(params, validateWizardStatusParams, "wizard.status", respond)) return;
const sessionId = params.sessionId;
const session = findWizardSessionOrRespond({
context,
respond,
sessionId
});
if (!session) return;
const status = readWizardStatus(session);
if (status.status !== "running") context.wizardSessions.delete(sessionId);
respond(true, status, void 0);
}
};
//#endregion
//#region src/gateway/server-methods.ts
const CONTROL_PLANE_WRITE_METHODS = new Set([
"config.apply",
"config.patch",
"update.run"
]);
function authorizeGatewayMethod(method, client) {
if (!client?.connect) return null;
if (method === "health") return null;
const roleRaw = client.connect.role ?? "operator";
const role = parseGatewayRole(roleRaw);
if (!role) return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${roleRaw}`);
const scopes = client.connect.scopes ?? [];
if (!isRoleAuthorizedForMethod(role, method)) return errorShape(ErrorCodes.INVALID_REQUEST, `unauthorized role: ${role}`);
if (role === "node") return null;
if (scopes.includes("operator.admin")) return null;
const scopeAuth = authorizeOperatorScopesForMethod(method, scopes);
if (!scopeAuth.allowed) return errorShape(ErrorCodes.INVALID_REQUEST, `missing scope: ${scopeAuth.missingScope}`);
return null;
}
const coreGatewayHandlers = {
...connectHandlers,
...logsHandlers,
...voicewakeHandlers,
...healthHandlers,
...channelsHandlers,
...chatHandlers,
...cronHandlers,
...deviceHandlers,
...doctorHandlers,
...execApprovalsHandlers,
...webHandlers,
...modelsHandlers,
...configHandlers,
...wizardHandlers,
...talkHandlers,
...toolsCatalogHandlers,
...ttsHandlers,
...skillsHandlers,
...sessionsHandlers,
...systemHandlers,
...updateHandlers,
...nodeHandlers,
...nodePendingHandlers,
...pushHandlers,
...sendHandlers,
...usageHandlers,
...agentHandlers,
...agentsHandlers,
...browserHandlers
};
async function handleGatewayRequest(opts) {
const { req, respond, client, isWebchatConnect, context } = opts;
const authError = authorizeGatewayMethod(req.method, client);
if (authError) {
respond(false, void 0, authError);
return;
}
if (CONTROL_PLANE_WRITE_METHODS.has(req.method)) {
const budget = consumeControlPlaneWriteBudget({ client });
if (!budget.allowed) {
const actor = resolveControlPlaneActor(client);
context.logGateway.warn(`control-plane write rate-limited method=${req.method} ${formatControlPlaneActor(actor)} retryAfterMs=${budget.retryAfterMs} key=${budget.key}`);
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, `rate limit exceeded for ${req.method}; retry after ${Math.ceil(budget.retryAfterMs / 1e3)}s`, {
retryable: true,
retryAfterMs: budget.retryAfterMs,
details: {
method: req.method,
limit: "3 per 60s"
}
}));
return;
}
}
const handler = opts.extraHandlers?.[req.method] ?? coreGatewayHandlers[req.method];
if (!handler) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `unknown method: ${req.method}`));
return;
}
const invokeHandler = () => handler({
req,
params: req.params ?? {},
client,
isWebchatConnect,
respond,
context
});
await withPluginRuntimeGatewayRequestScope({
context,
client,
isWebchatConnect
}, invokeHandler);
}
//#endregion
//#region src/gateway/server-methods/exec-approval.ts
function createExecApprovalHandlers(manager, opts) {
return {
"exec.approval.request": async ({ params, respond, context, client }) => {
if (!validateExecApprovalRequestParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid exec.approval.request params: ${formatValidationErrors(validateExecApprovalRequestParams.errors)}`));
return;
}
const p = params;
const twoPhase = p.twoPhase === true;
const timeoutMs = typeof p.timeoutMs === "number" ? p.timeoutMs : DEFAULT_EXEC_APPROVAL_TIMEOUT_MS;
const explicitId = typeof p.id === "string" && p.id.trim().length > 0 ? p.id.trim() : null;
const host = typeof p.host === "string" ? p.host.trim() : "";
const nodeId = typeof p.nodeId === "string" ? p.nodeId.trim() : "";
const approvalContext = resolveSystemRunApprovalRequestContext({
host,
command: p.command,
commandArgv: p.commandArgv,
systemRunPlan: p.systemRunPlan,
cwd: p.cwd,
agentId: p.agentId,
sessionKey: p.sessionKey
});
const effectiveCommandArgv = approvalContext.commandArgv;
const effectiveCwd = approvalContext.cwd;
const effectiveAgentId = approvalContext.agentId;
const effectiveSessionKey = approvalContext.sessionKey;
const effectiveCommandText = approvalContext.commandText;
if (host === "node" && !nodeId) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "nodeId is required for host=node"));
return;
}
if (host === "node" && !approvalContext.plan) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "systemRunPlan is required for host=node"));
return;
}
if (!effectiveCommandText) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "command is required"));
return;
}
if (host === "node" && (!Array.isArray(effectiveCommandArgv) || effectiveCommandArgv.length === 0)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "commandArgv is required for host=node"));
return;
}
const envBinding = buildSystemRunApprovalEnvBinding(p.env);
const systemRunBinding = host === "node" ? buildSystemRunApprovalBinding({
argv: effectiveCommandArgv,
cwd: effectiveCwd,
agentId: effectiveAgentId,
sessionKey: effectiveSessionKey,
env: p.env
}) : null;
if (explicitId && manager.getSnapshot(explicitId)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "approval id already pending"));
return;
}
const request = {
command: sanitizeExecApprovalDisplayText(effectiveCommandText),
commandPreview: host === "node" || !approvalContext.commandPreview ? void 0 : sanitizeExecApprovalDisplayText(approvalContext.commandPreview),
commandArgv: host === "node" ? void 0 : effectiveCommandArgv,
envKeys: envBinding.envKeys.length > 0 ? envBinding.envKeys : void 0,
systemRunBinding: systemRunBinding?.binding ?? null,
systemRunPlan: approvalContext.plan,
cwd: effectiveCwd ?? null,
nodeId: host === "node" ? nodeId : null,
host: host || null,
security: p.security ?? null,
ask: p.ask ?? null,
agentId: effectiveAgentId ?? null,
resolvedPath: p.resolvedPath ?? null,
sessionKey: effectiveSessionKey ?? null,
turnSourceChannel: typeof p.turnSourceChannel === "string" ? p.turnSourceChannel.trim() || null : null,
turnSourceTo: typeof p.turnSourceTo === "string" ? p.turnSourceTo.trim() || null : null,
turnSourceAccountId: typeof p.turnSourceAccountId === "string" ? p.turnSourceAccountId.trim() || null : null,
turnSourceThreadId: p.turnSourceThreadId ?? null
};
const record = manager.create(request, timeoutMs, explicitId);
record.requestedByConnId = client?.connId ?? null;
record.requestedByDeviceId = client?.connect?.device?.id ?? null;
record.requestedByClientId = client?.connect?.client?.id ?? null;
let decisionPromise;
try {
decisionPromise = manager.register(record, timeoutMs);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `registration failed: ${String(err)}`));
return;
}
context.broadcast("exec.approval.requested", {
id: record.id,
request: record.request,
createdAtMs: record.createdAtMs,
expiresAtMs: record.expiresAtMs
}, { dropIfSlow: true });
const hasExecApprovalClients = context.hasExecApprovalClients?.() ?? false;
let forwarded = false;
if (opts?.forwarder) try {
forwarded = await opts.forwarder.handleRequested({
id: record.id,
request: record.request,
createdAtMs: record.createdAtMs,
expiresAtMs: record.expiresAtMs
});
} catch (err) {
context.logGateway?.error?.(`exec approvals: forward request failed: ${String(err)}`);
}
if (!hasExecApprovalClients && !forwarded) {
manager.expire(record.id, "no-approval-route");
respond(true, {
id: record.id,
decision: null,
createdAtMs: record.createdAtMs,
expiresAtMs: record.expiresAtMs
}, void 0);
return;
}
if (twoPhase) respond(true, {
status: "accepted",
id: record.id,
createdAtMs: record.createdAtMs,
expiresAtMs: record.expiresAtMs
}, void 0);
const decision = await decisionPromise;
respond(true, {
id: record.id,
decision,
createdAtMs: record.createdAtMs,
expiresAtMs: record.expiresAtMs
}, void 0);
},
"exec.approval.waitDecision": async ({ params, respond }) => {
const p = params;
const id = typeof p.id === "string" ? p.id.trim() : "";
if (!id) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "id is required"));
return;
}
const decisionPromise = manager.awaitDecision(id);
if (!decisionPromise) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "approval expired or not found"));
return;
}
const snapshot = manager.getSnapshot(id);
respond(true, {
id,
decision: await decisionPromise,
createdAtMs: snapshot?.createdAtMs,
expiresAtMs: snapshot?.expiresAtMs
}, void 0);
},
"exec.approval.resolve": async ({ params, respond, client, context }) => {
if (!validateExecApprovalResolveParams(params)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid exec.approval.resolve params: ${formatValidationErrors(validateExecApprovalResolveParams.errors)}`));
return;
}
const p = params;
const decision = p.decision;
if (decision !== "allow-once" && decision !== "allow-always" && decision !== "deny") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid decision"));
return;
}
const resolvedId = manager.lookupPendingId(p.id);
if (resolvedId.kind === "none") {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown or expired approval id"));
return;
}
if (resolvedId.kind === "ambiguous") {
const candidates = resolvedId.ids.slice(0, 3).join(", ");
const remainder = resolvedId.ids.length > 3 ? ` (+${resolvedId.ids.length - 3} more)` : "";
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `ambiguous approval id prefix; matches: ${candidates}${remainder}. Use the full id.`));
return;
}
const approvalId = resolvedId.id;
const snapshot = manager.getSnapshot(approvalId);
const resolvedBy = client?.connect?.client?.displayName ?? client?.connect?.client?.id;
if (!manager.resolve(approvalId, decision, resolvedBy ?? null)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "unknown or expired approval id"));
return;
}
context.broadcast("exec.approval.resolved", {
id: approvalId,
decision,
resolvedBy,
ts: Date.now(),
request: snapshot?.request
}, { dropIfSlow: true });
opts?.forwarder?.handleResolved({
id: approvalId,
decision,
resolvedBy,
ts: Date.now(),
request: snapshot?.request
}).catch((err) => {
context.logGateway?.error?.(`exec approvals: forward resolve failed: ${String(err)}`);
});
respond(true, { ok: true }, void 0);
}
};
}
//#endregion
//#region src/gateway/server-methods/secrets.ts
function invalidSecretsResolveField(errors) {
for (const issue of errors ?? []) if (issue.instancePath === "/commandName" || issue.instancePath === "" && String(issue.params?.missingProperty) === "commandName") return "commandName";
return "targetIds";
}
function createSecretsHandlers(params) {
return {
"secrets.reload": async ({ respond }) => {
try {
respond(true, {
ok: true,
warningCount: (await params.reloadSecrets()).warningCount
});
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
}
},
"secrets.resolve": async ({ params: requestParams, respond }) => {
if (!validateSecretsResolveParams(requestParams)) {
const field = invalidSecretsResolveField(validateSecretsResolveParams.errors);
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid secrets.resolve params: ${field}`));
return;
}
const commandName = requestParams.commandName.trim();
if (!commandName) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid secrets.resolve params: commandName"));
return;
}
const targetIds = requestParams.targetIds.map((entry) => entry.trim()).filter((entry) => entry.length > 0);
for (const targetId of targetIds) if (!isKnownSecretTargetId(targetId)) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid secrets.resolve params: unknown target id "${String(targetId)}"`));
return;
}
try {
const result = await params.resolveSecrets({
commandName,
targetIds
});
const payload = {
ok: true,
assignments: result.assignments,
diagnostics: result.diagnostics,
inactiveRefPaths: result.inactiveRefPaths
};
if (!validateSecretsResolveResult(payload)) throw new Error("secrets.resolve returned invalid payload.");
respond(true, payload);
} catch (err) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, String(err)));
}
}
};
}
//#endregion
//#region src/gateway/server-mobile-nodes.ts
const isMobilePlatform = (platform) => {
const p = typeof platform === "string" ? platform.trim().toLowerCase() : "";
if (!p) return false;
return p.startsWith("ios") || p.startsWith("ipados") || p.startsWith("android");
};
function hasConnectedMobileNode(registry) {
return registry.listConnected().some((n) => isMobilePlatform(n.platform));
}
//#endregion
//#region src/gateway/server-model-catalog.ts
async function loadGatewayModelCatalog() {
return await loadModelCatalog({ config: loadConfig() });
}
//#endregion
//#region src/gateway/server-node-subscriptions.ts
function createNodeSubscriptionManager() {
const nodeSubscriptions = /* @__PURE__ */ new Map();
const sessionSubscribers = /* @__PURE__ */ new Map();
const toPayloadJSON = (payload) => payload ? JSON.stringify(payload) : null;
const subscribe = (nodeId, sessionKey) => {
const normalizedNodeId = nodeId.trim();
const normalizedSessionKey = sessionKey.trim();
if (!normalizedNodeId || !normalizedSessionKey) return;
let nodeSet = nodeSubscriptions.get(normalizedNodeId);
if (!nodeSet) {
nodeSet = /* @__PURE__ */ new Set();
nodeSubscriptions.set(normalizedNodeId, nodeSet);
}
if (nodeSet.has(normalizedSessionKey)) return;
nodeSet.add(normalizedSessionKey);
let sessionSet = sessionSubscribers.get(normalizedSessionKey);
if (!sessionSet) {
sessionSet = /* @__PURE__ */ new Set();
sessionSubscribers.set(normalizedSessionKey, sessionSet);
}
sessionSet.add(normalizedNodeId);
};
const unsubscribe = (nodeId, sessionKey) => {
const normalizedNodeId = nodeId.trim();
const normalizedSessionKey = sessionKey.trim();
if (!normalizedNodeId || !normalizedSessionKey) return;
const nodeSet = nodeSubscriptions.get(normalizedNodeId);
nodeSet?.delete(normalizedSessionKey);
if (nodeSet?.size === 0) nodeSubscriptions.delete(normalizedNodeId);
const sessionSet = sessionSubscribers.get(normalizedSessionKey);
sessionSet?.delete(normalizedNodeId);
if (sessionSet?.size === 0) sessionSubscribers.delete(normalizedSessionKey);
};
const unsubscribeAll = (nodeId) => {
const normalizedNodeId = nodeId.trim();
const nodeSet = nodeSubscriptions.get(normalizedNodeId);
if (!nodeSet) return;
for (const sessionKey of nodeSet) {
const sessionSet = sessionSubscribers.get(sessionKey);
sessionSet?.delete(normalizedNodeId);
if (sessionSet?.size === 0) sessionSubscribers.delete(sessionKey);
}
nodeSubscriptions.delete(normalizedNodeId);
};
const sendToSession = (sessionKey, event, payload, sendEvent) => {
const normalizedSessionKey = sessionKey.trim();
if (!normalizedSessionKey || !sendEvent) return;
const subs = sessionSubscribers.get(normalizedSessionKey);
if (!subs || subs.size === 0) return;
const payloadJSON = toPayloadJSON(payload);
for (const nodeId of subs) sendEvent({
nodeId,
event,
payloadJSON
});
};
const sendToAllSubscribed = (event, payload, sendEvent) => {
if (!sendEvent) return;
const payloadJSON = toPayloadJSON(payload);
for (const nodeId of nodeSubscriptions.keys()) sendEvent({
nodeId,
event,
payloadJSON
});
};
const sendToAllConnected = (event, payload, listConnected, sendEvent) => {
if (!sendEvent || !listConnected) return;
const payloadJSON = toPayloadJSON(payload);
for (const node of listConnected()) sendEvent({
nodeId: node.nodeId,
event,
payloadJSON
});
};
const clear = () => {
nodeSubscriptions.clear();
sessionSubscribers.clear();
};
return {
subscribe,
unsubscribe,
unsubscribeAll,
sendToSession,
sendToAllSubscribed,
sendToAllConnected,
clear
};
}
//#endregion
//#region src/gateway/server-plugins.ts
const fallbackGatewayContextState = resolveGlobalSingleton(Symbol.for("openclaw.fallbackGatewayContextState"), () => ({
context: void 0,
resolveContext: void 0
}));
function setFallbackGatewayContextResolver(resolveContext) {
fallbackGatewayContextState.resolveContext = resolveContext;
}
function getFallbackGatewayContext() {
return fallbackGatewayContextState.resolveContext?.() ?? fallbackGatewayContextState.context;
}
const pluginSubagentPolicyState = resolveGlobalSingleton(Symbol.for("openclaw.pluginSubagentOverridePolicyState"), () => ({ policies: {} }));
function normalizeAllowedModelRef(raw) {
const trimmed = raw.trim();
if (!trimmed) return null;
if (trimmed === "*") return "*";
const slash = trimmed.indexOf("/");
if (slash <= 0 || slash >= trimmed.length - 1) return null;
const providerRaw = trimmed.slice(0, slash).trim();
const modelRaw = trimmed.slice(slash + 1).trim();
if (!providerRaw || !modelRaw) return null;
const normalized = normalizeModelRef(providerRaw, modelRaw);
return `${normalized.provider}/${normalized.model}`;
}
function setPluginSubagentOverridePolicies(cfg) {
const normalized = normalizePluginsConfig(cfg.plugins);
const policies = {};
for (const [pluginId, entry] of Object.entries(normalized.entries)) {
const allowModelOverride = entry.subagent?.allowModelOverride === true;
const hasConfiguredAllowlist = entry.subagent?.hasAllowedModelsConfig === true;
const configuredAllowedModels = entry.subagent?.allowedModels ?? [];
const allowedModels = /* @__PURE__ */ new Set();
let allowAnyModel = false;
for (const modelRef of configuredAllowedModels) {
const normalizedModelRef = normalizeAllowedModelRef(modelRef);
if (!normalizedModelRef) continue;
if (normalizedModelRef === "*") {
allowAnyModel = true;
continue;
}
allowedModels.add(normalizedModelRef);
}
if (!allowModelOverride && !hasConfiguredAllowlist && allowedModels.size === 0 && !allowAnyModel) continue;
policies[pluginId] = {
allowModelOverride,
allowAnyModel,
hasConfiguredAllowlist,
allowedModels
};
}
pluginSubagentPolicyState.policies = policies;
}
function authorizeFallbackModelOverride(params) {
const pluginId = params.pluginId?.trim();
if (!pluginId) return {
allowed: false,
reason: "provider/model override requires plugin identity in fallback subagent runs."
};
const policy = pluginSubagentPolicyState.policies[pluginId];
if (!policy?.allowModelOverride) return {
allowed: false,
reason: `plugin "${pluginId}" is not trusted for fallback provider/model override requests. See https://docs.openclaw.ai/tools/plugin#runtime-helpers and search for: plugins.entries.<id>.subagent.allowModelOverride`
};
if (policy.allowAnyModel) return { allowed: true };
if (policy.hasConfiguredAllowlist && policy.allowedModels.size === 0) return {
allowed: false,
reason: `plugin "${pluginId}" configured subagent.allowedModels, but none of the entries normalized to a valid provider/model target.`
};
if (policy.allowedModels.size === 0) return { allowed: true };
const requestedModelRef = resolveRequestedFallbackModelRef(params);
if (!requestedModelRef) return {
allowed: false,
reason: "fallback provider/model overrides that use an allowlist must resolve to a canonical provider/model target."
};
if (policy.allowedModels.has(requestedModelRef)) return { allowed: true };
return {
allowed: false,
reason: `model override "${requestedModelRef}" is not allowlisted for plugin "${pluginId}".`
};
}
function resolveRequestedFallbackModelRef(params) {
if (params.provider && params.model) {
const normalizedRequest = normalizeModelRef(params.provider, params.model);
return `${normalizedRequest.provider}/${normalizedRequest.model}`;
}
const rawModel = params.model?.trim();
if (!rawModel || !rawModel.includes("/")) return null;
const parsed = parseModelRef(rawModel, "");
if (!parsed?.provider || !parsed.model) return null;
return `${parsed.provider}/${parsed.model}`;
}
function createSyntheticOperatorClient(params) {
return {
connect: {
minProtocol: 3,
maxProtocol: 3,
client: {
id: GATEWAY_CLIENT_IDS.GATEWAY_CLIENT,
version: "internal",
platform: "node",
mode: GATEWAY_CLIENT_MODES.BACKEND
},
role: "operator",
scopes: params?.scopes ?? ["operator.write"]
},
internal: { allowModelOverride: params?.allowModelOverride === true }
};
}
function hasAdminScope(client) {
return (Array.isArray(client?.connect?.scopes) ? client.connect.scopes : []).includes(ADMIN_SCOPE$2);
}
function canClientUseModelOverride(client) {
return hasAdminScope(client) || client?.internal?.allowModelOverride === true;
}
async function dispatchGatewayMethod(method, params, options) {
const scope = getPluginRuntimeGatewayRequestScope();
const context = scope?.context ?? getFallbackGatewayContext();
const isWebchatConnect = scope?.isWebchatConnect ?? (() => false);
if (!context) throw new Error(`Plugin subagent dispatch requires a gateway request scope (method: ${method}). No scope set and no fallback context available.`);
let result;
await handleGatewayRequest({
req: {
type: "req",
id: `plugin-subagent-${randomUUID()}`,
method,
params
},
client: scope?.client ?? createSyntheticOperatorClient({
allowModelOverride: options?.allowSyntheticModelOverride === true,
scopes: options?.syntheticScopes
}),
isWebchatConnect,
respond: (ok, payload, error) => {
if (!result) result = {
ok,
payload,
error
};
},
context
});
if (!result) throw new Error(`Gateway method "${method}" completed without a response.`);
if (!result.ok) throw new Error(result.error?.message ?? `Gateway method "${method}" failed.`);
return result.payload;
}
function createGatewaySubagentRuntime() {
const getSessionMessages = async (params) => {
const payload = await dispatchGatewayMethod("sessions.get", {
key: params.sessionKey,
...params.limit != null && { limit: params.limit }
});
return { messages: Array.isArray(payload?.messages) ? payload.messages : [] };
};
return {
async run(params) {
const scope = getPluginRuntimeGatewayRequestScope();
const overrideRequested = Boolean(params.provider || params.model);
const hasRequestScopeClient = Boolean(scope?.client);
let allowOverride = hasRequestScopeClient && canClientUseModelOverride(scope?.client ?? null);
let allowSyntheticModelOverride = false;
if (overrideRequested && !allowOverride && !hasRequestScopeClient) {
const fallbackAuth = authorizeFallbackModelOverride({
pluginId: scope?.pluginId,
provider: params.provider,
model: params.model
});
if (!fallbackAuth.allowed) throw new Error(fallbackAuth.reason);
allowOverride = true;
allowSyntheticModelOverride = true;
}
if (overrideRequested && !allowOverride) throw new Error("provider/model override is not authorized for this plugin subagent run.");
const runId = (await dispatchGatewayMethod("agent", {
sessionKey: params.sessionKey,
message: params.message,
deliver: params.deliver ?? false,
...allowOverride && params.provider && { provider: params.provider },
...allowOverride && params.model && { model: params.model },
...params.extraSystemPrompt && { extraSystemPrompt: params.extraSystemPrompt },
...params.lane && { lane: params.lane },
...params.idempotencyKey && { idempotencyKey: params.idempotencyKey }
}, { allowSyntheticModelOverride }))?.runId;
if (typeof runId !== "string" || !runId) throw new Error("Gateway agent method returned an invalid runId.");
return { runId };
},
async waitForRun(params) {
const payload = await dispatchGatewayMethod("agent.wait", {
runId: params.runId,
...params.timeoutMs != null && { timeoutMs: params.timeoutMs }
});
const status = payload?.status;
if (status !== "ok" && status !== "error" && status !== "timeout") throw new Error(`Gateway agent.wait returned unexpected status: ${status}`);
return {
status,
...typeof payload?.error === "string" && payload.error && { error: payload.error }
};
},
getSessionMessages,
async getSession(params) {
return getSessionMessages(params);
},
async deleteSession(params) {
await dispatchGatewayMethod("sessions.delete", {
key: params.sessionKey,
deleteTranscript: params.deleteTranscript ?? true
}, { syntheticScopes: [ADMIN_SCOPE$2] });
}
};
}
function loadGatewayPlugins(params) {
setPluginSubagentOverridePolicies(params.cfg);
setGatewaySubagentRuntime(createGatewaySubagentRuntime());
const pluginRegistry = loadOpenClawPlugins({
config: params.cfg,
workspaceDir: params.workspaceDir,
onlyPluginIds: resolveGatewayStartupPluginIds({
config: params.cfg,
workspaceDir: params.workspaceDir,
env: process.env
}),
logger: {
info: (msg) => params.log.info(msg),
warn: (msg) => params.log.warn(msg),
error: (msg) => params.log.error(msg),
debug: (msg) => params.log.debug(msg)
},
coreGatewayHandlers: params.coreGatewayHandlers,
runtimeOptions: { allowGatewaySubagentBinding: true },
preferSetupRuntimeForChannelPlugins: params.preferSetupRuntimeForChannelPlugins
});
primeConfiguredBindingRegistry({ cfg: params.cfg });
const pluginMethods = Object.keys(pluginRegistry.gatewayHandlers);
const gatewayMethods = Array.from(new Set([...params.baseMethods, ...pluginMethods]));
if ((params.logDiagnostics ?? true) && pluginRegistry.diagnostics.length > 0) for (const diag of pluginRegistry.diagnostics) {
const details = [diag.pluginId ? `plugin=${diag.pluginId}` : null, diag.source ? `source=${diag.source}` : null].filter((entry) => Boolean(entry)).join(", ");
const message = details ? `[plugins] ${diag.message} (${details})` : `[plugins] ${diag.message}`;
if (diag.level === "error") params.log.error(message);
else params.log.info(message);
}
return {
pluginRegistry,
gatewayMethods
};
}
//#endregion
//#region src/hooks/gmail-watcher-lifecycle.ts
async function startGmailWatcherWithLogs(params) {
if (isTruthyEnvValue(process.env.OPENCLAW_SKIP_GMAIL_WATCHER)) {
params.onSkipped?.();
return;
}
try {
const gmailResult = await startGmailWatcher(params.cfg);
if (gmailResult.started) {
params.log.info("gmail watcher started");
return;
}
if (gmailResult.reason && gmailResult.reason !== "hooks not enabled" && gmailResult.reason !== "no gmail account configured") params.log.warn(`gmail watcher not started: ${gmailResult.reason}`);
} catch (err) {
params.log.error(`gmail watcher failed to start: ${String(err)}`);
}
}
//#endregion
//#region src/hooks/module-loader.ts
function resolveFileModuleUrl(params) {
const url = pathToFileURL(params.modulePath).href;
if (!params.cacheBust) return url;
return `${url}?t=${params.nowMs ?? Date.now()}`;
}
async function importFileModule(params) {
return await import(resolveFileModuleUrl(params));
}
function resolveFunctionModuleExport(params) {
const explicitExport = params.exportName?.trim();
if (explicitExport) {
const candidate = params.mod[explicitExport];
return typeof candidate === "function" ? candidate : void 0;
}
const fallbacks = params.fallbackExportNames ?? ["default"];
for (const exportName of fallbacks) {
const candidate = params.mod[exportName];
if (typeof candidate === "function") return candidate;
}
}
//#endregion
//#region src/gateway/hooks-mapping.ts
const hookPresetMappings = { gmail: [{
id: "gmail",
match: { path: "gmail" },
action: "agent",
wakeMode: "now",
name: "Gmail",
sessionKey: "hook:gmail:{{messages[0].id}}",
messageTemplate: "New email from {{messages[0].from}}\nSubject: {{messages[0].subject}}\n{{messages[0].snippet}}\n{{messages[0].body}}"
}] };
const transformCache = /* @__PURE__ */ new Map();
function resolveHookMappings(hooks, opts) {
const presets = hooks?.presets ?? [];
const gmailAllowUnsafe = hooks?.gmail?.allowUnsafeExternalContent;
const mappings = [];
if (hooks?.mappings) mappings.push(...hooks.mappings);
for (const preset of presets) {
const presetMappings = hookPresetMappings[preset];
if (!presetMappings) continue;
if (preset === "gmail" && typeof gmailAllowUnsafe === "boolean") {
mappings.push(...presetMappings.map((mapping) => ({
...mapping,
allowUnsafeExternalContent: gmailAllowUnsafe
})));
continue;
}
mappings.push(...presetMappings);
}
if (mappings.length === 0) return [];
const configDir = path.resolve(opts?.configDir ?? path.dirname(CONFIG_PATH));
const transformsDir = resolveOptionalContainedPath(path.join(configDir, "hooks", "transforms"), hooks?.transformsDir, "Hook transformsDir");
return mappings.map((mapping, index) => normalizeHookMapping(mapping, index, transformsDir));
}
async function applyHookMappings(mappings, ctx) {
if (mappings.length === 0) return null;
for (const mapping of mappings) {
if (!mappingMatches(mapping, ctx)) continue;
const base = buildActionFromMapping(mapping, ctx);
if (!base.ok) return base;
let override = null;
if (mapping.transform) {
override = await (await loadTransform(mapping.transform))(ctx);
if (override === null) return {
ok: true,
action: null,
skipped: true
};
}
if (!base.action) return {
ok: true,
action: null,
skipped: true
};
const merged = mergeAction(base.action, override, mapping.action);
if (!merged.ok) return merged;
return merged;
}
return null;
}
function normalizeHookMapping(mapping, index, transformsDir) {
const id = mapping.id?.trim() || `mapping-${index + 1}`;
const matchPath = normalizeMatchPath(mapping.match?.path);
const matchSource = mapping.match?.source?.trim();
const action = mapping.action ?? "agent";
const wakeMode = mapping.wakeMode ?? "now";
const transform = mapping.transform ? {
modulePath: resolveContainedPath(transformsDir, mapping.transform.module, "Hook transform"),
exportName: mapping.transform.export?.trim() || void 0
} : void 0;
return {
id,
matchPath,
matchSource,
action,
wakeMode,
name: mapping.name,
agentId: mapping.agentId?.trim() || void 0,
sessionKey: mapping.sessionKey,
messageTemplate: mapping.messageTemplate,
textTemplate: mapping.textTemplate,
deliver: mapping.deliver,
allowUnsafeExternalContent: mapping.allowUnsafeExternalContent,
channel: mapping.channel,
to: mapping.to,
model: mapping.model,
thinking: mapping.thinking,
timeoutSeconds: mapping.timeoutSeconds,
transform
};
}
function mappingMatches(mapping, ctx) {
if (mapping.matchPath) {
if (mapping.matchPath !== normalizeMatchPath(ctx.path)) return false;
}
if (mapping.matchSource) {
const source = typeof ctx.payload.source === "string" ? ctx.payload.source : void 0;
if (!source || source !== mapping.matchSource) return false;
}
return true;
}
function buildActionFromMapping(mapping, ctx) {
if (mapping.action === "wake") return {
ok: true,
action: {
kind: "wake",
text: renderTemplate(mapping.textTemplate ?? "", ctx),
mode: mapping.wakeMode ?? "now"
}
};
return {
ok: true,
action: {
kind: "agent",
message: renderTemplate(mapping.messageTemplate ?? "", ctx),
name: renderOptional(mapping.name, ctx),
agentId: mapping.agentId,
wakeMode: mapping.wakeMode ?? "now",
sessionKey: renderOptional(mapping.sessionKey, ctx),
deliver: mapping.deliver,
allowUnsafeExternalContent: mapping.allowUnsafeExternalContent,
channel: mapping.channel,
to: renderOptional(mapping.to, ctx),
model: renderOptional(mapping.model, ctx),
thinking: renderOptional(mapping.thinking, ctx),
timeoutSeconds: mapping.timeoutSeconds
}
};
}
function mergeAction(base, override, defaultAction) {
if (!override) return validateAction(base);
if ((override.kind ?? base.kind ?? defaultAction) === "wake") {
const baseWake = base.kind === "wake" ? base : void 0;
return validateAction({
kind: "wake",
text: typeof override.text === "string" ? override.text : baseWake?.text ?? "",
mode: override.mode === "next-heartbeat" ? "next-heartbeat" : baseWake?.mode ?? "now"
});
}
const baseAgent = base.kind === "agent" ? base : void 0;
return validateAction({
kind: "agent",
message: typeof override.message === "string" ? override.message : baseAgent?.message ?? "",
wakeMode: override.wakeMode === "next-heartbeat" ? "next-heartbeat" : baseAgent?.wakeMode ?? "now",
name: override.name ?? baseAgent?.name,
agentId: override.agentId ?? baseAgent?.agentId,
sessionKey: override.sessionKey ?? baseAgent?.sessionKey,
deliver: typeof override.deliver === "boolean" ? override.deliver : baseAgent?.deliver,
allowUnsafeExternalContent: typeof override.allowUnsafeExternalContent === "boolean" ? override.allowUnsafeExternalContent : baseAgent?.allowUnsafeExternalContent,
channel: override.channel ?? baseAgent?.channel,
to: override.to ?? baseAgent?.to,
model: override.model ?? baseAgent?.model,
thinking: override.thinking ?? baseAgent?.thinking,
timeoutSeconds: override.timeoutSeconds ?? baseAgent?.timeoutSeconds
});
}
function validateAction(action) {
if (action.kind === "wake") {
if (!action.text?.trim()) return {
ok: false,
error: "hook mapping requires text"
};
return {
ok: true,
action
};
}
if (!action.message?.trim()) return {
ok: false,
error: "hook mapping requires message"
};
return {
ok: true,
action
};
}
async function loadTransform(transform) {
const cacheKey = `${transform.modulePath}::${transform.exportName ?? "default"}`;
const cached = transformCache.get(cacheKey);
if (cached) return cached;
const fn = resolveTransformFn(await importFileModule({ modulePath: transform.modulePath }), transform.exportName);
transformCache.set(cacheKey, fn);
return fn;
}
function resolveTransformFn(mod, exportName) {
const candidate = resolveFunctionModuleExport({
mod,
exportName,
fallbackExportNames: ["default", "transform"]
});
if (!candidate) throw new Error("hook transform module must export a function");
return candidate;
}
function resolvePath(baseDir, target) {
if (!target) return path.resolve(baseDir);
return path.isAbsolute(target) ? path.resolve(target) : path.resolve(baseDir, target);
}
function escapesBase(baseDir, candidate) {
const relative = path.relative(baseDir, candidate);
return relative === ".." || relative.startsWith(`..${path.sep}`) || path.isAbsolute(relative);
}
function safeRealpathSync(candidate) {
try {
const nativeRealpath = fs.realpathSync.native;
return nativeRealpath ? nativeRealpath(candidate) : fs.realpathSync(candidate);
} catch {
return null;
}
}
function resolveExistingAncestor(candidate) {
let current = path.resolve(candidate);
while (true) {
if (fs.existsSync(current)) return current;
const parent = path.dirname(current);
if (parent === current) return null;
current = parent;
}
}
function resolveContainedPath(baseDir, target, label) {
const base = path.resolve(baseDir);
const trimmed = target?.trim();
if (!trimmed) throw new Error(`${label} module path is required`);
const resolved = resolvePath(base, trimmed);
if (escapesBase(base, resolved)) throw new Error(`${label} module path must be within ${base}: ${target}`);
const baseRealpath = safeRealpathSync(base);
const existingAncestor = resolveExistingAncestor(resolved);
const existingAncestorRealpath = existingAncestor ? safeRealpathSync(existingAncestor) : null;
if (baseRealpath && existingAncestorRealpath && escapesBase(baseRealpath, existingAncestorRealpath)) throw new Error(`${label} module path must be within ${base}: ${target}`);
return resolved;
}
function resolveOptionalContainedPath(baseDir, target, label) {
const trimmed = target?.trim();
if (!trimmed) return path.resolve(baseDir);
return resolveContainedPath(baseDir, trimmed, label);
}
function normalizeMatchPath(raw) {
if (!raw) return;
const trimmed = raw.trim();
if (!trimmed) return;
return trimmed.replace(/^\/+/, "").replace(/\/+$/, "");
}
function renderOptional(value, ctx) {
if (!value) return;
const rendered = renderTemplate(value, ctx).trim();
return rendered ? rendered : void 0;
}
function renderTemplate(template, ctx) {
if (!template) return "";
return template.replace(/\{\{\s*([^}]+)\s*\}\}/g, (_, expr) => {
const value = resolveTemplateExpr(expr.trim(), ctx);
if (value === void 0 || value === null) return "";
if (typeof value === "string") return value;
if (typeof value === "number" || typeof value === "boolean") return String(value);
return JSON.stringify(value);
});
}
function resolveTemplateExpr(expr, ctx) {
if (expr === "path") return ctx.path;
if (expr === "now") return (/* @__PURE__ */ new Date()).toISOString();
if (expr.startsWith("headers.")) return getByPath(ctx.headers, expr.slice(8));
if (expr.startsWith("query.")) return getByPath(Object.fromEntries(ctx.url.searchParams.entries()), expr.slice(6));
if (expr.startsWith("payload.")) return getByPath(ctx.payload, expr.slice(8));
return getByPath(ctx.payload, expr);
}
const BLOCKED_PATH_KEYS = new Set([
"__proto__",
"prototype",
"constructor"
]);
function getByPath(input, pathExpr) {
if (!pathExpr) return;
const parts = [];
const re = /([^.[\]]+)|(\[(\d+)\])/g;
let match = re.exec(pathExpr);
while (match) {
if (match[1]) parts.push(match[1]);
else if (match[3]) parts.push(Number(match[3]));
match = re.exec(pathExpr);
}
let current = input;
for (const part of parts) {
if (current === null || current === void 0) return;
if (typeof part === "number") {
if (!Array.isArray(current)) return;
current = current[part];
continue;
}
if (BLOCKED_PATH_KEYS.has(part)) return;
if (typeof current !== "object") return;
current = current[part];
}
return current;
}
//#endregion
//#region src/gateway/hooks.ts
const DEFAULT_HOOKS_PATH = "/hooks";
const DEFAULT_HOOKS_MAX_BODY_BYTES = 256 * 1024;
const MAX_HOOK_IDEMPOTENCY_KEY_LENGTH = 256;
function resolveHooksConfig(cfg) {
if (cfg.hooks?.enabled !== true) return null;
const token = cfg.hooks?.token?.trim();
if (!token) throw new Error("hooks.enabled requires hooks.token");
const rawPath = cfg.hooks?.path?.trim() || DEFAULT_HOOKS_PATH;
const withSlash = rawPath.startsWith("/") ? rawPath : `/${rawPath}`;
const trimmed = withSlash.length > 1 ? withSlash.replace(/\/+$/, "") : withSlash;
if (trimmed === "/") throw new Error("hooks.path may not be '/'");
const maxBodyBytes = cfg.hooks?.maxBodyBytes && cfg.hooks.maxBodyBytes > 0 ? cfg.hooks.maxBodyBytes : DEFAULT_HOOKS_MAX_BODY_BYTES;
const mappings = resolveHookMappings(cfg.hooks);
const defaultAgentId = resolveDefaultAgentId(cfg);
const knownAgentIds = resolveKnownAgentIds(cfg, defaultAgentId);
const allowedAgentIds = resolveAllowedAgentIds(cfg.hooks?.allowedAgentIds);
const defaultSessionKey = resolveSessionKey$1(cfg.hooks?.defaultSessionKey);
const allowedSessionKeyPrefixes = resolveAllowedSessionKeyPrefixes(cfg.hooks?.allowedSessionKeyPrefixes);
if (defaultSessionKey && allowedSessionKeyPrefixes && !isSessionKeyAllowedByPrefix(defaultSessionKey, allowedSessionKeyPrefixes)) throw new Error("hooks.defaultSessionKey must match hooks.allowedSessionKeyPrefixes");
if (!defaultSessionKey && allowedSessionKeyPrefixes && !isSessionKeyAllowedByPrefix("hook:example", allowedSessionKeyPrefixes)) throw new Error("hooks.allowedSessionKeyPrefixes must include 'hook:' when hooks.defaultSessionKey is unset");
return {
basePath: trimmed,
token,
maxBodyBytes,
mappings,
agentPolicy: {
defaultAgentId,
knownAgentIds,
allowedAgentIds
},
sessionPolicy: {
defaultSessionKey,
allowRequestSessionKey: cfg.hooks?.allowRequestSessionKey === true,
allowedSessionKeyPrefixes
}
};
}
function resolveKnownAgentIds(cfg, defaultAgentId) {
const known = new Set(listAgentIds(cfg));
known.add(defaultAgentId);
return known;
}
function resolveSessionKey$1(raw) {
const value = raw?.trim();
return value ? value : void 0;
}
function normalizeSessionKeyPrefix(raw) {
const value = raw.trim().toLowerCase();
return value ? value : void 0;
}
function resolveAllowedSessionKeyPrefixes(raw) {
if (!Array.isArray(raw)) return;
const set = /* @__PURE__ */ new Set();
for (const prefix of raw) {
const normalized = normalizeSessionKeyPrefix(prefix);
if (!normalized) continue;
set.add(normalized);
}
return set.size > 0 ? Array.from(set) : void 0;
}
function isSessionKeyAllowedByPrefix(sessionKey, prefixes) {
const normalized = sessionKey.trim().toLowerCase();
if (!normalized) return false;
return prefixes.some((prefix) => normalized.startsWith(prefix));
}
function extractHookToken(req) {
const auth = typeof req.headers.authorization === "string" ? req.headers.authorization.trim() : "";
if (auth.toLowerCase().startsWith("bearer ")) {
const token = auth.slice(7).trim();
if (token) return token;
}
const headerToken = typeof req.headers["x-openclaw-token"] === "string" ? req.headers["x-openclaw-token"].trim() : "";
if (headerToken) return headerToken;
}
async function readJsonBody(req, maxBytes) {
const result = await readJsonBodyWithLimit(req, {
maxBytes,
emptyObjectOnEmpty: true
});
if (result.ok) return result;
if (result.code === "PAYLOAD_TOO_LARGE") return {
ok: false,
error: "payload too large"
};
if (result.code === "REQUEST_BODY_TIMEOUT") return {
ok: false,
error: "request body timeout"
};
if (result.code === "CONNECTION_CLOSED") return {
ok: false,
error: requestBodyErrorToText("CONNECTION_CLOSED")
};
return {
ok: false,
error: result.error
};
}
function normalizeHookHeaders(req) {
const headers = {};
for (const [key, value] of Object.entries(req.headers)) if (typeof value === "string") headers[key.toLowerCase()] = value;
else if (Array.isArray(value) && value.length > 0) headers[key.toLowerCase()] = value.join(", ");
return headers;
}
function normalizeWakePayload(payload) {
const text = typeof payload.text === "string" ? payload.text.trim() : "";
if (!text) return {
ok: false,
error: "text required"
};
return {
ok: true,
value: {
text,
mode: payload.mode === "next-heartbeat" ? "next-heartbeat" : "now"
}
};
}
const listHookChannelValues = () => ["last", ...listChannelPlugins().map((plugin) => plugin.id)];
const getHookChannelSet = () => new Set(listHookChannelValues());
const getHookChannelError = () => `channel must be ${listHookChannelValues().join("|")}`;
function resolveHookChannel(raw) {
if (raw === void 0) return "last";
if (typeof raw !== "string") return null;
const normalized = normalizeMessageChannel(raw);
if (!normalized || !getHookChannelSet().has(normalized)) return null;
return normalized;
}
function resolveHookDeliver(raw) {
return raw !== false;
}
function resolveOptionalHookIdempotencyKey(raw) {
if (typeof raw !== "string") return;
const trimmed = raw.trim();
if (!trimmed || trimmed.length > MAX_HOOK_IDEMPOTENCY_KEY_LENGTH) return;
return trimmed;
}
function resolveHookIdempotencyKey(params) {
return resolveOptionalHookIdempotencyKey(params.headers?.["idempotency-key"]) || resolveOptionalHookIdempotencyKey(params.headers?.["x-openclaw-idempotency-key"]) || resolveOptionalHookIdempotencyKey(params.payload.idempotencyKey);
}
function resolveHookTargetAgentId(hooksConfig, agentId) {
const raw = agentId?.trim();
if (!raw) return;
const normalized = normalizeAgentId(raw);
if (hooksConfig.agentPolicy.knownAgentIds.has(normalized)) return normalized;
return hooksConfig.agentPolicy.defaultAgentId;
}
function isHookAgentAllowed(hooksConfig, agentId) {
const raw = agentId?.trim();
if (!raw) return true;
const allowed = hooksConfig.agentPolicy.allowedAgentIds;
if (allowed === void 0) return true;
const resolved = resolveHookTargetAgentId(hooksConfig, raw);
return resolved ? allowed.has(resolved) : false;
}
const getHookAgentPolicyError = () => "agentId is not allowed by hooks.allowedAgentIds";
const getHookSessionKeyRequestPolicyError = () => "sessionKey is disabled for external /hooks/agent payloads; set hooks.allowRequestSessionKey=true to enable";
const getHookSessionKeyPrefixError = (prefixes) => `sessionKey must start with one of: ${prefixes.join(", ")}`;
function resolveHookSessionKey(params) {
const requested = resolveSessionKey$1(params.sessionKey);
if (requested) {
if (params.source === "request" && !params.hooksConfig.sessionPolicy.allowRequestSessionKey) return {
ok: false,
error: getHookSessionKeyRequestPolicyError()
};
const allowedPrefixes = params.hooksConfig.sessionPolicy.allowedSessionKeyPrefixes;
if (allowedPrefixes && !isSessionKeyAllowedByPrefix(requested, allowedPrefixes)) return {
ok: false,
error: getHookSessionKeyPrefixError(allowedPrefixes)
};
return {
ok: true,
value: requested
};
}
const defaultSessionKey = params.hooksConfig.sessionPolicy.defaultSessionKey;
if (defaultSessionKey) return {
ok: true,
value: defaultSessionKey
};
const generated = `hook:${(params.idFactory ?? randomUUID)()}`;
const allowedPrefixes = params.hooksConfig.sessionPolicy.allowedSessionKeyPrefixes;
if (allowedPrefixes && !isSessionKeyAllowedByPrefix(generated, allowedPrefixes)) return {
ok: false,
error: getHookSessionKeyPrefixError(allowedPrefixes)
};
return {
ok: true,
value: generated
};
}
function normalizeHookDispatchSessionKey(params) {
const trimmed = params.sessionKey.trim();
if (!trimmed || !params.targetAgentId) return trimmed;
const parsed = parseAgentSessionKey(trimmed);
if (!parsed) return trimmed;
const targetAgentId = normalizeAgentId(params.targetAgentId);
if (parsed.agentId !== targetAgentId) return `agent:${parsed.agentId}:${parsed.rest}`;
return parsed.rest;
}
function normalizeAgentPayload(payload) {
const message = typeof payload.message === "string" ? payload.message.trim() : "";
if (!message) return {
ok: false,
error: "message required"
};
const nameRaw = payload.name;
const name = typeof nameRaw === "string" && nameRaw.trim() ? nameRaw.trim() : "Hook";
const agentIdRaw = payload.agentId;
const agentId = typeof agentIdRaw === "string" && agentIdRaw.trim() ? agentIdRaw.trim() : void 0;
const idempotencyKey = resolveOptionalHookIdempotencyKey(payload.idempotencyKey);
const wakeMode = payload.wakeMode === "next-heartbeat" ? "next-heartbeat" : "now";
const sessionKeyRaw = payload.sessionKey;
const sessionKey = typeof sessionKeyRaw === "string" && sessionKeyRaw.trim() ? sessionKeyRaw.trim() : void 0;
const channel = resolveHookChannel(payload.channel);
if (!channel) return {
ok: false,
error: getHookChannelError()
};
const toRaw = payload.to;
const to = typeof toRaw === "string" && toRaw.trim() ? toRaw.trim() : void 0;
const modelRaw = payload.model;
const model = typeof modelRaw === "string" && modelRaw.trim() ? modelRaw.trim() : void 0;
if (modelRaw !== void 0 && !model) return {
ok: false,
error: "model required"
};
const deliver = resolveHookDeliver(payload.deliver);
const thinkingRaw = payload.thinking;
const thinking = typeof thinkingRaw === "string" && thinkingRaw.trim() ? thinkingRaw.trim() : void 0;
const timeoutRaw = payload.timeoutSeconds;
return {
ok: true,
value: {
message,
name,
agentId,
idempotencyKey,
wakeMode,
sessionKey,
deliver,
channel,
to,
model,
thinking,
timeoutSeconds: typeof timeoutRaw === "number" && Number.isFinite(timeoutRaw) && timeoutRaw > 0 ? Math.floor(timeoutRaw) : void 0
}
};
}
//#endregion
//#region src/gateway/server-browser.ts
async function startBrowserControlServerIfEnabled() {
if (isTruthyEnvValue(process.env.OPENCLAW_SKIP_BROWSER_CONTROL_SERVER)) return null;
const override = process.env.OPENCLAW_BROWSER_CONTROL_MODULE?.trim();
const mod = override ? await import(override) : await import("./server-BE4iUfS6.js");
const start = typeof mod.startBrowserControlServiceFromConfig === "function" ? mod.startBrowserControlServiceFromConfig : mod.startBrowserControlServerFromConfig;
const stop = typeof mod.stopBrowserControlService === "function" ? mod.stopBrowserControlService : mod.stopBrowserControlServer;
if (!start) return null;
await start();
return { stop: stop ?? (async () => {}) };
}
//#endregion
//#region src/canvas-host/file-resolver.ts
function normalizeUrlPath(rawPath) {
const decoded = decodeURIComponent(rawPath || "/");
const normalized = path.posix.normalize(decoded);
return normalized.startsWith("/") ? normalized : `/${normalized}`;
}
async function resolveFileWithinRoot(rootReal, urlPath) {
const normalized = normalizeUrlPath(urlPath);
const rel = normalized.replace(/^\/+/, "");
if (rel.split("/").some((p) => p === "..")) return null;
const tryOpen = async (relative) => {
try {
return await openFileWithinRoot({
rootDir: rootReal,
relativePath: relative
});
} catch (err) {
if (err instanceof SafeOpenError) return null;
throw err;
}
};
if (normalized.endsWith("/")) return await tryOpen(path.posix.join(rel, "index.html"));
const candidate = path.join(rootReal, rel);
try {
const st = await fs$1.lstat(candidate);
if (st.isSymbolicLink()) return null;
if (st.isDirectory()) return await tryOpen(path.posix.join(rel, "index.html"));
} catch {}
return await tryOpen(rel);
}
//#endregion
//#region src/canvas-host/a2ui.ts
const A2UI_PATH = "/__openclaw__/a2ui";
const CANVAS_HOST_PATH = "/__openclaw__/canvas";
const CANVAS_WS_PATH = "/__openclaw__/ws";
let cachedA2uiRootReal;
let resolvingA2uiRoot = null;
let cachedA2uiResolvedAtMs = 0;
const A2UI_ROOT_RETRY_NULL_AFTER_MS = 1e4;
async function resolveA2uiRoot() {
const here = path.dirname(fileURLToPath(import.meta.url));
const entryDir = process.argv[1] ? path.dirname(path.resolve(process.argv[1])) : null;
const candidates = [
path.resolve(here, "a2ui"),
path.resolve(here, "canvas-host/a2ui"),
path.resolve(here, "../canvas-host/a2ui"),
...entryDir ? [
path.resolve(entryDir, "a2ui"),
path.resolve(entryDir, "canvas-host/a2ui"),
path.resolve(entryDir, "../canvas-host/a2ui")
] : [],
path.resolve(here, "../../src/canvas-host/a2ui"),
path.resolve(here, "../src/canvas-host/a2ui"),
path.resolve(process.cwd(), "src/canvas-host/a2ui"),
path.resolve(process.cwd(), "dist/canvas-host/a2ui")
];
if (process.execPath) candidates.unshift(path.resolve(path.dirname(process.execPath), "a2ui"));
for (const dir of candidates) try {
const indexPath = path.join(dir, "index.html");
const bundlePath = path.join(dir, "a2ui.bundle.js");
await fs$1.stat(indexPath);
await fs$1.stat(bundlePath);
return dir;
} catch {}
return null;
}
async function resolveA2uiRootReal() {
if (cachedA2uiRootReal !== void 0 && (cachedA2uiRootReal !== null || Date.now() - cachedA2uiResolvedAtMs < A2UI_ROOT_RETRY_NULL_AFTER_MS)) return cachedA2uiRootReal;
if (!resolvingA2uiRoot) resolvingA2uiRoot = (async () => {
const root = await resolveA2uiRoot();
cachedA2uiRootReal = root ? await fs$1.realpath(root) : null;
cachedA2uiResolvedAtMs = Date.now();
resolvingA2uiRoot = null;
return cachedA2uiRootReal;
})();
return resolvingA2uiRoot;
}
function injectCanvasLiveReload(html) {
const snippet = `
<script>
(() => {
// Cross-platform action bridge helper.
// Works on:
// - iOS: window.webkit.messageHandlers.openclawCanvasA2UIAction.postMessage(...)
// - Android: window.openclawCanvasA2UIAction.postMessage(...)
const handlerNames = ["openclawCanvasA2UIAction"];
function postToNode(payload) {
try {
const raw = typeof payload === "string" ? payload : JSON.stringify(payload);
for (const name of handlerNames) {
const iosHandler = globalThis.webkit?.messageHandlers?.[name];
if (iosHandler && typeof iosHandler.postMessage === "function") {
iosHandler.postMessage(raw);
return true;
}
const androidHandler = globalThis[name];
if (androidHandler && typeof androidHandler.postMessage === "function") {
// Important: call as a method on the interface object (binding matters on Android WebView).
androidHandler.postMessage(raw);
return true;
}
}
} catch {}
return false;
}
function sendUserAction(userAction) {
const id =
(userAction && typeof userAction.id === "string" && userAction.id.trim()) ||
(globalThis.crypto?.randomUUID?.() ?? String(Date.now()));
const action = { ...userAction, id };
return postToNode({ userAction: action });
}
globalThis.OpenClaw = globalThis.OpenClaw ?? {};
globalThis.OpenClaw.postMessage = postToNode;
globalThis.OpenClaw.sendUserAction = sendUserAction;
globalThis.openclawPostMessage = postToNode;
globalThis.openclawSendUserAction = sendUserAction;
try {
const cap = new URLSearchParams(location.search).get("oc_cap");
const proto = location.protocol === "https:" ? "wss" : "ws";
const capQuery = cap ? "?oc_cap=" + encodeURIComponent(cap) : "";
const ws = new WebSocket(proto + "://" + location.host + ${JSON.stringify(CANVAS_WS_PATH)} + capQuery);
ws.onmessage = (ev) => {
if (String(ev.data || "") === "reload") location.reload();
};
} catch {}
})();
<\/script>
`.trim();
const idx = html.toLowerCase().lastIndexOf("</body>");
if (idx >= 0) return `${html.slice(0, idx)}\n${snippet}\n${html.slice(idx)}`;
return `${html}\n${snippet}\n`;
}
async function handleA2uiHttpRequest(req, res) {
const urlRaw = req.url;
if (!urlRaw) return false;
const url = new URL(urlRaw, "http://localhost");
const basePath = url.pathname === "/__openclaw__/a2ui" || url.pathname.startsWith(`/__openclaw__/a2ui/`) ? A2UI_PATH : void 0;
if (!basePath) return false;
if (req.method !== "GET" && req.method !== "HEAD") {
res.statusCode = 405;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Method Not Allowed");
return true;
}
const a2uiRootReal = await resolveA2uiRootReal();
if (!a2uiRootReal) {
res.statusCode = 503;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("A2UI assets not found");
return true;
}
const result = await resolveFileWithinRoot(a2uiRootReal, url.pathname.slice(basePath.length) || "/");
if (!result) {
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("not found");
return true;
}
try {
const lower = result.realPath.toLowerCase();
const mime = lower.endsWith(".html") || lower.endsWith(".htm") ? "text/html" : await detectMime({ filePath: result.realPath }) ?? "application/octet-stream";
res.setHeader("Cache-Control", "no-store");
if (req.method === "HEAD") {
res.setHeader("Content-Type", mime === "text/html" ? "text/html; charset=utf-8" : mime);
res.end();
return true;
}
if (mime === "text/html") {
const buf = await result.handle.readFile({ encoding: "utf8" });
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(injectCanvasLiveReload(buf));
return true;
}
res.setHeader("Content-Type", mime);
res.end(await result.handle.readFile());
return true;
} finally {
await result.handle.close().catch(() => {});
}
}
//#endregion
//#region src/gateway/control-ui-csp.ts
/**
* Compute SHA-256 CSP hashes for inline `<script>` blocks in an HTML string.
* Only scripts without a `src` attribute are considered inline.
*/
function computeInlineScriptHashes(html) {
const hashes = [];
const re = /<script(?:\s[^>]*)?>([^]*?)<\/script>/gi;
let match;
while ((match = re.exec(html)) !== null) {
const openTag = match[0].slice(0, match[0].indexOf(">") + 1);
if (/\bsrc\s*=/i.test(openTag)) continue;
const content = match[1];
if (!content) continue;
const hash = createHash("sha256").update(content, "utf8").digest("base64");
hashes.push(`sha256-${hash}`);
}
return hashes;
}
function buildControlUiCspHeader(opts) {
const hashes = opts?.inlineScriptHashes;
return [
"default-src 'self'",
"base-uri 'none'",
"object-src 'none'",
"frame-ancestors 'none'",
hashes?.length ? `script-src 'self' ${hashes.map((h) => `'${h}'`).join(" ")}` : "script-src 'self'",
"style-src 'self' 'unsafe-inline' https://fonts.googleapis.com",
"img-src 'self' data: https:",
"font-src 'self' https://fonts.gstatic.com",
"connect-src 'self' ws: wss:"
].join("; ");
}
//#endregion
//#region src/gateway/control-ui-http-utils.ts
function isReadHttpMethod(method) {
return method === "GET" || method === "HEAD";
}
function respondPlainText(res, statusCode, body) {
res.statusCode = statusCode;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end(body);
}
function respondNotFound(res) {
respondPlainText(res, 404, "Not Found");
}
//#endregion
//#region src/gateway/control-ui-routing.ts
const ROOT_MOUNTED_GATEWAY_PROBE_PATHS = new Set([
"/health",
"/healthz",
"/ready",
"/readyz"
]);
function classifyControlUiRequest(params) {
const { basePath, pathname, search, method } = params;
if (!basePath) {
if (pathname === "/ui" || pathname.startsWith("/ui/")) return { kind: "not-found" };
if (ROOT_MOUNTED_GATEWAY_PROBE_PATHS.has(pathname)) return { kind: "not-control-ui" };
if (pathname === "/plugins" || pathname.startsWith("/plugins/")) return { kind: "not-control-ui" };
if (pathname === "/api" || pathname.startsWith("/api/")) return { kind: "not-control-ui" };
if (!isReadHttpMethod(method)) return { kind: "not-control-ui" };
return { kind: "serve" };
}
if (!pathname.startsWith(`${basePath}/`) && pathname !== basePath) return { kind: "not-control-ui" };
if (!isReadHttpMethod(method)) return { kind: "not-control-ui" };
if (pathname === basePath) return {
kind: "redirect",
location: `${basePath}/${search}`
};
return { kind: "serve" };
}
//#endregion
//#region src/gateway/control-ui.ts
const ROOT_PREFIX = "/";
const CONTROL_UI_ASSETS_MISSING_MESSAGE = "Control UI assets not found. Build them with `pnpm ui:build` (auto-installs UI deps), or run `pnpm ui:dev` during development.";
function contentTypeForExt(ext) {
switch (ext) {
case ".html": return "text/html; charset=utf-8";
case ".js": return "application/javascript; charset=utf-8";
case ".css": return "text/css; charset=utf-8";
case ".json":
case ".map": return "application/json; charset=utf-8";
case ".svg": return "image/svg+xml";
case ".png": return "image/png";
case ".jpg":
case ".jpeg": return "image/jpeg";
case ".gif": return "image/gif";
case ".webp": return "image/webp";
case ".ico": return "image/x-icon";
case ".txt": return "text/plain; charset=utf-8";
default: return "application/octet-stream";
}
}
/**
* Extensions recognised as static assets. Missing files with these extensions
* return 404 instead of the SPA index.html fallback. `.html` is intentionally
* excluded — actual HTML files on disk are served earlier, and missing `.html`
* paths should fall through to the SPA router (client-side routers may use
* `.html`-suffixed routes).
*/
const STATIC_ASSET_EXTENSIONS = new Set([
".js",
".css",
".json",
".map",
".svg",
".png",
".jpg",
".jpeg",
".gif",
".webp",
".ico",
".txt"
]);
function applyControlUiSecurityHeaders(res) {
res.setHeader("X-Frame-Options", "DENY");
res.setHeader("Content-Security-Policy", buildControlUiCspHeader());
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("Referrer-Policy", "no-referrer");
}
function sendJson$2(res, status, body) {
res.statusCode = status;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.setHeader("Cache-Control", "no-cache");
res.end(JSON.stringify(body));
}
function respondControlUiAssetsUnavailable(res, options) {
if (options?.configuredRootPath) {
respondPlainText(res, 503, `Control UI assets not found at ${options.configuredRootPath}. Build them with \`pnpm ui:build\` (auto-installs UI deps), or update gateway.controlUi.root.`);
return;
}
respondPlainText(res, 503, CONTROL_UI_ASSETS_MISSING_MESSAGE);
}
function respondHeadForFile(req, res, filePath) {
if (req.method !== "HEAD") return false;
res.statusCode = 200;
setStaticFileHeaders(res, filePath);
res.end();
return true;
}
function isValidAgentId(agentId) {
return /^[a-z0-9][a-z0-9_-]{0,63}$/i.test(agentId);
}
function handleControlUiAvatarRequest(req, res, opts) {
const urlRaw = req.url;
if (!urlRaw) return false;
if (!isReadHttpMethod(req.method)) return false;
const url = new URL(urlRaw, "http://localhost");
const basePath = normalizeControlUiBasePath(opts.basePath);
const pathname = url.pathname;
const pathWithBase = basePath ? `${basePath}${CONTROL_UI_AVATAR_PREFIX}/` : `${CONTROL_UI_AVATAR_PREFIX}/`;
if (!pathname.startsWith(pathWithBase)) return false;
applyControlUiSecurityHeaders(res);
const agentIdParts = pathname.slice(pathWithBase.length).split("/").filter(Boolean);
const agentId = agentIdParts[0] ?? "";
if (agentIdParts.length !== 1 || !agentId || !isValidAgentId(agentId)) {
respondNotFound(res);
return true;
}
if (url.searchParams.get("meta") === "1") {
const resolved = opts.resolveAvatar(agentId);
sendJson$2(res, 200, { avatarUrl: resolved.kind === "local" ? buildControlUiAvatarUrl(basePath, agentId) : resolved.kind === "remote" || resolved.kind === "data" ? resolved.url : null });
return true;
}
const resolved = opts.resolveAvatar(agentId);
if (resolved.kind !== "local") {
respondNotFound(res);
return true;
}
const safeAvatar = resolveSafeAvatarFile(resolved.filePath);
if (!safeAvatar) {
respondNotFound(res);
return true;
}
try {
if (respondHeadForFile(req, res, safeAvatar.path)) return true;
serveResolvedFile(res, safeAvatar.path, fs.readFileSync(safeAvatar.fd));
return true;
} finally {
fs.closeSync(safeAvatar.fd);
}
}
function setStaticFileHeaders(res, filePath) {
const ext = path.extname(filePath).toLowerCase();
res.setHeader("Content-Type", contentTypeForExt(ext));
res.setHeader("Cache-Control", "no-cache");
}
function serveResolvedFile(res, filePath, body) {
setStaticFileHeaders(res, filePath);
res.end(body);
}
function serveResolvedIndexHtml(res, body) {
const hashes = computeInlineScriptHashes(body);
if (hashes.length > 0) res.setHeader("Content-Security-Policy", buildControlUiCspHeader({ inlineScriptHashes: hashes }));
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.setHeader("Cache-Control", "no-cache");
res.end(body);
}
function isExpectedSafePathError(error) {
const code = typeof error === "object" && error !== null && "code" in error ? String(error.code) : "";
return code === "ENOENT" || code === "ENOTDIR" || code === "ELOOP";
}
function resolveSafeAvatarFile(filePath) {
const opened = openVerifiedFileSync({
filePath,
rejectPathSymlink: true,
maxBytes: AVATAR_MAX_BYTES
});
if (!opened.ok) return null;
return {
path: opened.path,
fd: opened.fd
};
}
function resolveSafeControlUiFile(rootReal, filePath, rejectHardlinks) {
const opened = openBoundaryFileSync({
absolutePath: filePath,
rootPath: rootReal,
rootRealPath: rootReal,
boundaryLabel: "control ui root",
skipLexicalRootCheck: true,
rejectHardlinks
});
if (!opened.ok) return matchBoundaryFileOpenFailure(opened, {
io: (failure) => {
throw failure.error;
},
fallback: () => null
});
return {
path: opened.path,
fd: opened.fd
};
}
function isSafeRelativePath(relPath) {
if (!relPath) return false;
const normalized = path.posix.normalize(relPath);
if (path.posix.isAbsolute(normalized) || path.win32.isAbsolute(normalized)) return false;
if (normalized.startsWith("../") || normalized === "..") return false;
if (normalized.includes("\0")) return false;
return true;
}
function handleControlUiHttpRequest(req, res, opts) {
const urlRaw = req.url;
if (!urlRaw) return false;
const url = new URL(urlRaw, "http://localhost");
const basePath = normalizeControlUiBasePath(opts?.basePath);
const pathname = url.pathname;
const route = classifyControlUiRequest({
basePath,
pathname,
search: url.search,
method: req.method
});
if (route.kind === "not-control-ui") return false;
if (route.kind === "not-found") {
applyControlUiSecurityHeaders(res);
respondNotFound(res);
return true;
}
if (route.kind === "redirect") {
applyControlUiSecurityHeaders(res);
res.statusCode = 302;
res.setHeader("Location", route.location);
res.end();
return true;
}
applyControlUiSecurityHeaders(res);
if (pathname === (basePath ? `${basePath}/__openclaw/control-ui-config.json` : "/__openclaw/control-ui-config.json")) {
const config = opts?.config;
const identity = config ? resolveAssistantIdentity({
cfg: config,
agentId: opts?.agentId
}) : DEFAULT_ASSISTANT_IDENTITY;
const avatarValue = resolveAssistantAvatarUrl({
avatar: identity.avatar,
agentId: identity.agentId,
basePath
});
if (req.method === "HEAD") {
res.statusCode = 200;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.setHeader("Cache-Control", "no-cache");
res.end();
return true;
}
sendJson$2(res, 200, {
basePath,
assistantName: identity.name,
assistantAvatar: avatarValue ?? identity.avatar,
assistantAgentId: identity.agentId,
serverVersion: resolveRuntimeServiceVersion(process.env)
});
return true;
}
const rootState = opts?.root;
if (rootState?.kind === "invalid") {
respondControlUiAssetsUnavailable(res, { configuredRootPath: rootState.path });
return true;
}
if (rootState?.kind === "missing") {
respondControlUiAssetsUnavailable(res);
return true;
}
const root = rootState?.kind === "resolved" || rootState?.kind === "bundled" ? rootState.path : resolveControlUiRootSync({
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
});
if (!root) {
respondControlUiAssetsUnavailable(res);
return true;
}
const rootReal = (() => {
try {
return fs.realpathSync(root);
} catch (error) {
if (isExpectedSafePathError(error)) return null;
throw error;
}
})();
if (!rootReal) {
respondControlUiAssetsUnavailable(res);
return true;
}
const uiPath = basePath && pathname.startsWith(`${basePath}/`) ? pathname.slice(basePath.length) : pathname;
const rel = (() => {
if (uiPath === ROOT_PREFIX) return "";
const assetsIndex = uiPath.indexOf("/assets/");
if (assetsIndex >= 0) return uiPath.slice(assetsIndex + 1);
return uiPath.slice(1);
})();
const fileRel = (rel && !rel.endsWith("/") ? rel : `${rel}index.html`) || "index.html";
if (!isSafeRelativePath(fileRel)) {
respondNotFound(res);
return true;
}
const filePath = path.resolve(root, fileRel);
if (!isWithinDir(root, filePath)) {
respondNotFound(res);
return true;
}
const rejectHardlinks = !(rootState?.kind === "bundled" || rootState === void 0 && isPackageProvenControlUiRootSync(root, {
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
}));
const safeFile = resolveSafeControlUiFile(rootReal, filePath, rejectHardlinks);
if (safeFile) try {
if (respondHeadForFile(req, res, safeFile.path)) return true;
if (path.basename(safeFile.path) === "index.html") {
serveResolvedIndexHtml(res, fs.readFileSync(safeFile.fd, "utf8"));
return true;
}
serveResolvedFile(res, safeFile.path, fs.readFileSync(safeFile.fd));
return true;
} finally {
fs.closeSync(safeFile.fd);
}
if (STATIC_ASSET_EXTENSIONS.has(path.extname(fileRel).toLowerCase())) {
respondNotFound(res);
return true;
}
const safeIndex = resolveSafeControlUiFile(rootReal, path.join(root, "index.html"), rejectHardlinks);
if (safeIndex) try {
if (respondHeadForFile(req, res, safeIndex.path)) return true;
serveResolvedIndexHtml(res, fs.readFileSync(safeIndex.fd, "utf8"));
return true;
} finally {
fs.closeSync(safeIndex.fd);
}
respondNotFound(res);
return true;
}
//#endregion
//#region src/gateway/http-common.ts
/**
* Apply baseline security headers that are safe for all response types (API JSON,
* HTML pages, static assets, SSE streams). Headers that restrict framing or set a
* Content-Security-Policy are intentionally omitted here because some handlers
* (canvas host, A2UI) serve content that may be loaded inside frames.
*/
function setDefaultSecurityHeaders(res, opts) {
res.setHeader("X-Content-Type-Options", "nosniff");
res.setHeader("Referrer-Policy", "no-referrer");
res.setHeader("Permissions-Policy", "camera=(), microphone=(), geolocation=()");
const strictTransportSecurity = opts?.strictTransportSecurity;
if (typeof strictTransportSecurity === "string" && strictTransportSecurity.length > 0) res.setHeader("Strict-Transport-Security", strictTransportSecurity);
}
function sendJson$1(res, status, body) {
res.statusCode = status;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.end(JSON.stringify(body));
}
function sendText(res, status, body) {
res.statusCode = status;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end(body);
}
function sendMethodNotAllowed(res, allow = "POST") {
res.setHeader("Allow", allow);
sendText(res, 405, "Method Not Allowed");
}
function sendUnauthorized(res) {
sendJson$1(res, 401, { error: {
message: "Unauthorized",
type: "unauthorized"
} });
}
function sendRateLimited(res, retryAfterMs) {
if (retryAfterMs && retryAfterMs > 0) res.setHeader("Retry-After", String(Math.ceil(retryAfterMs / 1e3)));
sendJson$1(res, 429, { error: {
message: "Too many failed authentication attempts. Please try again later.",
type: "rate_limited"
} });
}
function sendGatewayAuthFailure(res, authResult) {
if (authResult.rateLimited) {
sendRateLimited(res, authResult.retryAfterMs);
return;
}
sendUnauthorized(res);
}
function sendInvalidRequest(res, message) {
sendJson$1(res, 400, { error: {
message,
type: "invalid_request_error"
} });
}
async function readJsonBodyOrError(req, res, maxBytes) {
const body = await readJsonBody(req, maxBytes);
if (!body.ok) {
if (body.error === "payload too large") {
sendJson$1(res, 413, { error: {
message: "Payload too large",
type: "invalid_request_error"
} });
return;
}
if (body.error === "request body timeout") {
sendJson$1(res, 408, { error: {
message: "Request body timeout",
type: "invalid_request_error"
} });
return;
}
sendInvalidRequest(res, body.error);
return;
}
return body.value;
}
function writeDone(res) {
res.write("data: [DONE]\n\n");
}
function setSseHeaders(res) {
res.statusCode = 200;
res.setHeader("Content-Type", "text/event-stream; charset=utf-8");
res.setHeader("Cache-Control", "no-cache");
res.setHeader("Connection", "keep-alive");
res.flushHeaders?.();
}
//#endregion
//#region src/gateway/http-utils.ts
function getHeader(req, name) {
const raw = req.headers[name.toLowerCase()];
if (typeof raw === "string") return raw;
if (Array.isArray(raw)) return raw[0];
}
function getBearerToken(req) {
const raw = getHeader(req, "authorization")?.trim() ?? "";
if (!raw.toLowerCase().startsWith("bearer ")) return;
return raw.slice(7).trim() || void 0;
}
function resolveAgentIdFromHeader(req) {
const raw = getHeader(req, "x-openclaw-agent-id")?.trim() || getHeader(req, "x-openclaw-agent")?.trim() || "";
if (!raw) return;
return normalizeAgentId(raw);
}
function resolveAgentIdFromModel(model) {
const raw = model?.trim();
if (!raw) return;
const agentId = (raw.match(/^openclaw[:/](?<agentId>[a-z0-9][a-z0-9_-]{0,63})$/i) ?? raw.match(/^agent:(?<agentId>[a-z0-9][a-z0-9_-]{0,63})$/i))?.groups?.agentId;
if (!agentId) return;
return normalizeAgentId(agentId);
}
function resolveAgentIdForRequest(params) {
const fromHeader = resolveAgentIdFromHeader(params.req);
if (fromHeader) return fromHeader;
return resolveAgentIdFromModel(params.model) ?? "main";
}
function resolveSessionKey(params) {
const explicit = getHeader(params.req, "x-openclaw-session-key")?.trim();
if (explicit) return explicit;
const user = params.user?.trim();
const mainKey = user ? `${params.prefix}-user:${user}` : `${params.prefix}:${randomUUID()}`;
return buildAgentMainSessionKey({
agentId: params.agentId,
mainKey
});
}
function resolveGatewayRequestContext(params) {
const agentId = resolveAgentIdForRequest({
req: params.req,
model: params.model
});
return {
agentId,
sessionKey: resolveSessionKey({
req: params.req,
agentId,
user: params.user,
prefix: params.sessionPrefix
}),
messageChannel: params.useMessageChannelHeader ? normalizeMessageChannel(getHeader(params.req, "x-openclaw-message-channel")) ?? params.defaultMessageChannel : params.defaultMessageChannel
};
}
//#endregion
//#region src/gateway/agent-event-assistant-text.ts
function resolveAssistantStreamDeltaText(evt) {
const delta = evt.data.delta;
const text = evt.data.text;
return typeof delta === "string" ? delta : typeof text === "string" ? text : "";
}
//#endregion
//#region src/gateway/agent-prompt.ts
/**
* Coerce body to string. Handles cases where body is a content array
* (e.g. [{type:"text", text:"hello"}]) that would serialize as
* [object Object] if used directly in a template literal.
*/
function safeBody(body) {
if (typeof body === "string") return body;
return extractTextFromChatContent(body) ?? "";
}
function buildAgentMessageFromConversationEntries(entries) {
if (entries.length === 0) return "";
let currentIndex = -1;
for (let i = entries.length - 1; i >= 0; i -= 1) {
const role = entries[i]?.role;
if (role === "user" || role === "tool") {
currentIndex = i;
break;
}
}
if (currentIndex < 0) currentIndex = entries.length - 1;
const currentEntry = entries[currentIndex]?.entry;
if (!currentEntry) return "";
const historyEntries = entries.slice(0, currentIndex).map((e) => e.entry);
if (historyEntries.length === 0) return safeBody(currentEntry.body);
const formatEntry = (entry) => `${entry.sender}: ${safeBody(entry.body)}`;
return buildHistoryContextFromEntries({
entries: [...historyEntries, currentEntry],
currentMessage: formatEntry(currentEntry),
formatEntry
});
}
//#endregion
//#region src/gateway/http-auth-helpers.ts
async function authorizeGatewayBearerRequestOrReply(params) {
const token = getBearerToken(params.req);
const authResult = await authorizeHttpGatewayConnect({
auth: params.auth,
connectAuth: token ? {
token,
password: token
} : null,
req: params.req,
trustedProxies: params.trustedProxies,
allowRealIpFallback: params.allowRealIpFallback,
rateLimiter: params.rateLimiter
});
if (!authResult.ok) {
sendGatewayAuthFailure(params.res, authResult);
return false;
}
return true;
}
//#endregion
//#region src/gateway/http-endpoint-helpers.ts
async function handleGatewayPostJsonEndpoint(req, res, opts) {
if (new URL(req.url ?? "/", `http://${req.headers.host || "localhost"}`).pathname !== opts.pathname) return false;
if (req.method !== "POST") {
sendMethodNotAllowed(res);
return;
}
if (!await authorizeGatewayBearerRequestOrReply({
req,
res,
auth: opts.auth,
trustedProxies: opts.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback,
rateLimiter: opts.rateLimiter
})) return;
const body = await readJsonBodyOrError(req, res, opts.maxBodyBytes);
if (body === void 0) return;
return { body };
}
//#endregion
//#region src/gateway/input-allowlist.ts
/**
* Normalize optional gateway URL-input hostname allowlists.
*
* Semantics are intentionally:
* - missing / empty / whitespace-only list => no hostname allowlist restriction
* - deny-all URL fetching => use the corresponding `allowUrl: false` switch
*/
function normalizeInputHostnameAllowlist(values) {
if (!values || values.length === 0) return;
const normalized = values.map((value) => value.trim()).filter((value) => value.length > 0);
return normalized.length > 0 ? normalized : void 0;
}
//#endregion
//#region src/gateway/openai-http.ts
const DEFAULT_OPENAI_CHAT_COMPLETIONS_BODY_BYTES = 20 * 1024 * 1024;
const IMAGE_ONLY_USER_MESSAGE = "User sent image(s) with no text.";
const DEFAULT_OPENAI_MAX_IMAGE_PARTS = 8;
const DEFAULT_OPENAI_MAX_TOTAL_IMAGE_BYTES = 20 * 1024 * 1024;
const DEFAULT_OPENAI_IMAGE_LIMITS = {
allowUrl: false,
allowedMimes: new Set(DEFAULT_INPUT_IMAGE_MIMES),
maxBytes: DEFAULT_INPUT_IMAGE_MAX_BYTES,
maxRedirects: 3,
timeoutMs: DEFAULT_INPUT_TIMEOUT_MS
};
function resolveOpenAiChatCompletionsLimits(config) {
const imageConfig = config?.images;
return {
maxBodyBytes: config?.maxBodyBytes ?? DEFAULT_OPENAI_CHAT_COMPLETIONS_BODY_BYTES,
maxImageParts: typeof config?.maxImageParts === "number" ? Math.max(0, Math.floor(config.maxImageParts)) : DEFAULT_OPENAI_MAX_IMAGE_PARTS,
maxTotalImageBytes: typeof config?.maxTotalImageBytes === "number" ? Math.max(1, Math.floor(config.maxTotalImageBytes)) : DEFAULT_OPENAI_MAX_TOTAL_IMAGE_BYTES,
images: {
allowUrl: imageConfig?.allowUrl ?? DEFAULT_OPENAI_IMAGE_LIMITS.allowUrl,
urlAllowlist: normalizeInputHostnameAllowlist(imageConfig?.urlAllowlist),
allowedMimes: normalizeMimeList(imageConfig?.allowedMimes, DEFAULT_INPUT_IMAGE_MIMES),
maxBytes: imageConfig?.maxBytes ?? 10485760,
maxRedirects: imageConfig?.maxRedirects ?? 3,
timeoutMs: imageConfig?.timeoutMs ?? 1e4
}
};
}
function writeSse(res, data) {
res.write(`data: ${JSON.stringify(data)}\n\n`);
}
function buildAgentCommandInput(params) {
return {
message: params.prompt.message,
extraSystemPrompt: params.prompt.extraSystemPrompt,
images: params.prompt.images,
sessionKey: params.sessionKey,
runId: params.runId,
deliver: false,
messageChannel: params.messageChannel,
bestEffortDeliver: false,
senderIsOwner: true,
allowModelOverride: true
};
}
function writeAssistantRoleChunk(res, params) {
writeSse(res, {
id: params.runId,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1e3),
model: params.model,
choices: [{
index: 0,
delta: { role: "assistant" }
}]
});
}
function writeAssistantContentChunk(res, params) {
writeSse(res, {
id: params.runId,
object: "chat.completion.chunk",
created: Math.floor(Date.now() / 1e3),
model: params.model,
choices: [{
index: 0,
delta: { content: params.content },
finish_reason: params.finishReason
}]
});
}
function asMessages(val) {
return Array.isArray(val) ? val : [];
}
function extractTextContent$1(content) {
if (typeof content === "string") return content;
if (Array.isArray(content)) return content.map((part) => {
if (!part || typeof part !== "object") return "";
const type = part.type;
const text = part.text;
const inputText = part.input_text;
if (type === "text" && typeof text === "string") return text;
if (type === "input_text" && typeof text === "string") return text;
if (typeof inputText === "string") return inputText;
return "";
}).filter(Boolean).join("\n");
return "";
}
function resolveImageUrlPart(part) {
if (!part || typeof part !== "object") return;
const imageUrl = part.image_url;
if (typeof imageUrl === "string") {
const trimmed = imageUrl.trim();
return trimmed.length > 0 ? trimmed : void 0;
}
if (!imageUrl || typeof imageUrl !== "object") return;
const rawUrl = imageUrl.url;
if (typeof rawUrl !== "string") return;
const trimmed = rawUrl.trim();
return trimmed.length > 0 ? trimmed : void 0;
}
function extractImageUrls(content) {
if (!Array.isArray(content)) return [];
const urls = [];
for (const part of content) {
if (!part || typeof part !== "object") continue;
if (part.type !== "image_url") continue;
const url = resolveImageUrlPart(part);
if (url) urls.push(url);
}
return urls;
}
function parseImageUrlToSource(url) {
const dataUriMatch = /^data:([^,]*?),(.*)$/is.exec(url);
if (dataUriMatch) {
const metadata = dataUriMatch[1]?.trim() ?? "";
const data = dataUriMatch[2] ?? "";
const metadataParts = metadata.split(";").map((part) => part.trim()).filter(Boolean);
if (!metadataParts.some((part) => part.toLowerCase() === "base64")) throw new Error("image_url data URI must be base64 encoded");
if (!data.trim()) throw new Error("image_url data URI is missing payload data");
return {
type: "base64",
mediaType: metadataParts.find((part) => part.includes("/")),
data
};
}
return {
type: "url",
url
};
}
function resolveActiveTurnContext(messagesUnknown) {
const messages = asMessages(messagesUnknown);
for (let i = messages.length - 1; i >= 0; i -= 1) {
const msg = messages[i];
if (!msg || typeof msg !== "object") continue;
const role = typeof msg.role === "string" ? msg.role.trim() : "";
const normalizedRole = role === "function" ? "tool" : role;
if (normalizedRole !== "user" && normalizedRole !== "tool") continue;
return {
activeTurnIndex: i,
activeUserMessageIndex: normalizedRole === "user" ? i : -1,
urls: normalizedRole === "user" ? extractImageUrls(msg.content) : []
};
}
return {
activeTurnIndex: -1,
activeUserMessageIndex: -1,
urls: []
};
}
async function resolveImagesForRequest(activeTurnContext, limits) {
const urls = activeTurnContext.urls;
if (urls.length === 0) return [];
if (urls.length > limits.maxImageParts) throw new Error(`Too many image_url parts (${urls.length}; limit ${limits.maxImageParts})`);
const images = [];
let totalBytes = 0;
for (const url of urls) {
const source = parseImageUrlToSource(url);
if (source.type === "base64") {
const sourceBytes = estimateBase64DecodedBytes(source.data);
if (totalBytes + sourceBytes > limits.maxTotalImageBytes) throw new Error(`Total image payload too large (${totalBytes + sourceBytes}; limit ${limits.maxTotalImageBytes})`);
}
const image = await extractImageContentFromSource(source, limits.images);
totalBytes += estimateBase64DecodedBytes(image.data);
if (totalBytes > limits.maxTotalImageBytes) throw new Error(`Total image payload too large (${totalBytes}; limit ${limits.maxTotalImageBytes})`);
images.push(image);
}
return images;
}
function buildAgentPrompt$1(messagesUnknown, activeUserMessageIndex) {
const messages = asMessages(messagesUnknown);
const systemParts = [];
const conversationEntries = [];
for (const [i, msg] of messages.entries()) {
if (!msg || typeof msg !== "object") continue;
const role = typeof msg.role === "string" ? msg.role.trim() : "";
const content = extractTextContent$1(msg.content).trim();
const hasImage = extractImageUrls(msg.content).length > 0;
if (!role) continue;
if (role === "system" || role === "developer") {
if (content) systemParts.push(content);
continue;
}
const normalizedRole = role === "function" ? "tool" : role;
if (normalizedRole !== "user" && normalizedRole !== "assistant" && normalizedRole !== "tool") continue;
const messageContent = normalizedRole === "user" && !content && hasImage && i === activeUserMessageIndex ? IMAGE_ONLY_USER_MESSAGE : content;
if (!messageContent) continue;
const name = typeof msg.name === "string" ? msg.name.trim() : "";
const sender = normalizedRole === "assistant" ? "Assistant" : normalizedRole === "user" ? "User" : name ? `Tool:${name}` : "Tool";
conversationEntries.push({
role: normalizedRole,
entry: {
sender,
body: messageContent
}
});
}
return {
message: buildAgentMessageFromConversationEntries(conversationEntries),
extraSystemPrompt: systemParts.length > 0 ? systemParts.join("\n\n") : void 0
};
}
function coerceRequest(val) {
if (!val || typeof val !== "object") return {};
return val;
}
function resolveAgentResponseText(result) {
const payloads = result?.payloads;
if (!Array.isArray(payloads) || payloads.length === 0) return "No response from OpenClaw.";
return payloads.map((p) => typeof p.text === "string" ? p.text : "").filter(Boolean).join("\n\n") || "No response from OpenClaw.";
}
async function handleOpenAiHttpRequest(req, res, opts) {
const limits = resolveOpenAiChatCompletionsLimits(opts.config);
const handled = await handleGatewayPostJsonEndpoint(req, res, {
pathname: "/v1/chat/completions",
auth: opts.auth,
trustedProxies: opts.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback,
rateLimiter: opts.rateLimiter,
maxBodyBytes: opts.maxBodyBytes ?? limits.maxBodyBytes
});
if (handled === false) return false;
if (!handled) return true;
const payload = coerceRequest(handled.body);
const stream = Boolean(payload.stream);
const model = typeof payload.model === "string" ? payload.model : "openclaw";
const { sessionKey, messageChannel } = resolveGatewayRequestContext({
req,
model,
user: typeof payload.user === "string" ? payload.user : void 0,
sessionPrefix: "openai",
defaultMessageChannel: "webchat",
useMessageChannelHeader: true
});
const activeTurnContext = resolveActiveTurnContext(payload.messages);
const prompt = buildAgentPrompt$1(payload.messages, activeTurnContext.activeUserMessageIndex);
let images = [];
try {
images = await resolveImagesForRequest(activeTurnContext, limits);
} catch (err) {
logWarn(`openai-compat: invalid image_url content: ${String(err)}`);
sendJson$1(res, 400, { error: {
message: "Invalid image_url content in `messages`.",
type: "invalid_request_error"
} });
return true;
}
if (!prompt.message && images.length === 0) {
sendJson$1(res, 400, { error: {
message: "Missing user message in `messages`.",
type: "invalid_request_error"
} });
return true;
}
const runId = `chatcmpl_${randomUUID()}`;
const deps = createDefaultDeps();
const commandInput = buildAgentCommandInput({
prompt: {
message: prompt.message,
extraSystemPrompt: prompt.extraSystemPrompt,
images: images.length > 0 ? images : void 0
},
sessionKey,
runId,
messageChannel
});
if (!stream) {
try {
const content = resolveAgentResponseText(await agentCommandFromIngress(commandInput, defaultRuntime, deps));
sendJson$1(res, 200, {
id: runId,
object: "chat.completion",
created: Math.floor(Date.now() / 1e3),
model,
choices: [{
index: 0,
message: {
role: "assistant",
content
},
finish_reason: "stop"
}],
usage: {
prompt_tokens: 0,
completion_tokens: 0,
total_tokens: 0
}
});
} catch (err) {
logWarn(`openai-compat: chat completion failed: ${String(err)}`);
sendJson$1(res, 500, { error: {
message: "internal error",
type: "api_error"
} });
}
return true;
}
setSseHeaders(res);
let wroteRole = false;
let sawAssistantDelta = false;
let closed = false;
const unsubscribe = onAgentEvent((evt) => {
if (evt.runId !== runId) return;
if (closed) return;
if (evt.stream === "assistant") {
const content = resolveAssistantStreamDeltaText(evt) ?? "";
if (!content) return;
if (!wroteRole) {
wroteRole = true;
writeAssistantRoleChunk(res, {
runId,
model
});
}
sawAssistantDelta = true;
writeAssistantContentChunk(res, {
runId,
model,
content,
finishReason: null
});
return;
}
if (evt.stream === "lifecycle") {
const phase = evt.data?.phase;
if (phase === "end" || phase === "error") {
closed = true;
unsubscribe();
writeDone(res);
res.end();
}
}
});
req.on("close", () => {
closed = true;
unsubscribe();
});
(async () => {
try {
const result = await agentCommandFromIngress(commandInput, defaultRuntime, deps);
if (closed) return;
if (!sawAssistantDelta) {
if (!wroteRole) {
wroteRole = true;
writeAssistantRoleChunk(res, {
runId,
model
});
}
const content = resolveAgentResponseText(result);
sawAssistantDelta = true;
writeAssistantContentChunk(res, {
runId,
model,
content,
finishReason: null
});
}
} catch (err) {
logWarn(`openai-compat: streaming chat completion failed: ${String(err)}`);
if (closed) return;
writeAssistantContentChunk(res, {
runId,
model,
content: "Error: internal error",
finishReason: "stop"
});
emitAgentEvent({
runId,
stream: "lifecycle",
data: { phase: "error" }
});
} finally {
if (!closed) {
closed = true;
unsubscribe();
writeDone(res);
res.end();
}
}
})();
return true;
}
//#endregion
//#region src/gateway/open-responses.schema.ts
/**
* OpenResponses API Zod Schemas
*
* Zod schemas for the OpenResponses `/v1/responses` endpoint.
* This module is isolated from gateway imports to enable future codegen and prevent drift.
*
* @see https://www.open-responses.com/
*/
const InputTextContentPartSchema = z.object({
type: z.literal("input_text"),
text: z.string()
}).strict();
const OutputTextContentPartSchema = z.object({
type: z.literal("output_text"),
text: z.string()
}).strict();
const InputImageSourceSchema = z.discriminatedUnion("type", [z.object({
type: z.literal("url"),
url: z.string().url()
}), z.object({
type: z.literal("base64"),
media_type: z.enum([
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
"image/heic",
"image/heif"
]),
data: z.string().min(1)
})]);
const InputImageContentPartSchema = z.object({
type: z.literal("input_image"),
source: InputImageSourceSchema
}).strict();
const InputFileSourceSchema = z.discriminatedUnion("type", [z.object({
type: z.literal("url"),
url: z.string().url()
}), z.object({
type: z.literal("base64"),
media_type: z.string().min(1),
data: z.string().min(1),
filename: z.string().optional()
})]);
const InputFileContentPartSchema = z.object({
type: z.literal("input_file"),
source: InputFileSourceSchema
}).strict();
const ContentPartSchema = z.discriminatedUnion("type", [
InputTextContentPartSchema,
OutputTextContentPartSchema,
InputImageContentPartSchema,
InputFileContentPartSchema
]);
const MessageItemRoleSchema = z.enum([
"system",
"developer",
"user",
"assistant"
]);
const MessageItemSchema = z.object({
type: z.literal("message"),
role: MessageItemRoleSchema,
content: z.union([z.string(), z.array(ContentPartSchema)])
}).strict();
const FunctionCallItemSchema = z.object({
type: z.literal("function_call"),
id: z.string().optional(),
call_id: z.string().optional(),
name: z.string(),
arguments: z.string()
}).strict();
const FunctionCallOutputItemSchema = z.object({
type: z.literal("function_call_output"),
call_id: z.string(),
output: z.string()
}).strict();
const ReasoningItemSchema = z.object({
type: z.literal("reasoning"),
content: z.string().optional(),
encrypted_content: z.string().optional(),
summary: z.string().optional()
}).strict();
const ItemReferenceItemSchema = z.object({
type: z.literal("item_reference"),
id: z.string()
}).strict();
const ItemParamSchema = z.discriminatedUnion("type", [
MessageItemSchema,
FunctionCallItemSchema,
FunctionCallOutputItemSchema,
ReasoningItemSchema,
ItemReferenceItemSchema
]);
const ToolDefinitionSchema = z.object({
type: z.literal("function"),
function: z.object({
name: z.string().min(1, "Tool name cannot be empty"),
description: z.string().optional(),
parameters: z.record(z.string(), z.unknown()).optional()
})
}).strict();
const ToolChoiceSchema = z.union([
z.literal("auto"),
z.literal("none"),
z.literal("required"),
z.object({
type: z.literal("function"),
function: z.object({ name: z.string() })
})
]);
const CreateResponseBodySchema = z.object({
model: z.string(),
input: z.union([z.string(), z.array(ItemParamSchema)]),
instructions: z.string().optional(),
tools: z.array(ToolDefinitionSchema).optional(),
tool_choice: ToolChoiceSchema.optional(),
stream: z.boolean().optional(),
max_output_tokens: z.number().int().positive().optional(),
max_tool_calls: z.number().int().positive().optional(),
user: z.string().optional(),
temperature: z.number().optional(),
top_p: z.number().optional(),
metadata: z.record(z.string(), z.string()).optional(),
store: z.boolean().optional(),
previous_response_id: z.string().optional(),
reasoning: z.object({
effort: z.enum([
"low",
"medium",
"high"
]).optional(),
summary: z.enum([
"auto",
"concise",
"detailed"
]).optional()
}).optional(),
truncation: z.enum(["auto", "disabled"]).optional()
}).strict();
const ResponseStatusSchema = z.enum([
"in_progress",
"completed",
"failed",
"cancelled",
"incomplete"
]);
const OutputItemSchema = z.discriminatedUnion("type", [
z.object({
type: z.literal("message"),
id: z.string(),
role: z.literal("assistant"),
content: z.array(OutputTextContentPartSchema),
status: z.enum(["in_progress", "completed"]).optional()
}).strict(),
z.object({
type: z.literal("function_call"),
id: z.string(),
call_id: z.string(),
name: z.string(),
arguments: z.string(),
status: z.enum(["in_progress", "completed"]).optional()
}).strict(),
z.object({
type: z.literal("reasoning"),
id: z.string(),
content: z.string().optional(),
summary: z.string().optional()
}).strict()
]);
const UsageSchema = z.object({
input_tokens: z.number().int().nonnegative(),
output_tokens: z.number().int().nonnegative(),
total_tokens: z.number().int().nonnegative()
});
const ResponseResourceSchema = z.object({
id: z.string(),
object: z.literal("response"),
created_at: z.number().int(),
status: ResponseStatusSchema,
model: z.string(),
output: z.array(OutputItemSchema),
usage: UsageSchema,
error: z.object({
code: z.string(),
message: z.string()
}).optional()
});
z.object({
type: z.literal("response.created"),
response: ResponseResourceSchema
});
z.object({
type: z.literal("response.in_progress"),
response: ResponseResourceSchema
});
z.object({
type: z.literal("response.completed"),
response: ResponseResourceSchema
});
z.object({
type: z.literal("response.failed"),
response: ResponseResourceSchema
});
z.object({
type: z.literal("response.output_item.added"),
output_index: z.number().int().nonnegative(),
item: OutputItemSchema
});
z.object({
type: z.literal("response.output_item.done"),
output_index: z.number().int().nonnegative(),
item: OutputItemSchema
});
z.object({
type: z.literal("response.content_part.added"),
item_id: z.string(),
output_index: z.number().int().nonnegative(),
content_index: z.number().int().nonnegative(),
part: OutputTextContentPartSchema
});
z.object({
type: z.literal("response.content_part.done"),
item_id: z.string(),
output_index: z.number().int().nonnegative(),
content_index: z.number().int().nonnegative(),
part: OutputTextContentPartSchema
});
z.object({
type: z.literal("response.output_text.delta"),
item_id: z.string(),
output_index: z.number().int().nonnegative(),
content_index: z.number().int().nonnegative(),
delta: z.string()
});
z.object({
type: z.literal("response.output_text.done"),
item_id: z.string(),
output_index: z.number().int().nonnegative(),
content_index: z.number().int().nonnegative(),
text: z.string()
});
//#endregion
//#region src/gateway/openresponses-prompt.ts
function extractTextContent(content) {
if (typeof content === "string") return content;
return content.map((part) => {
if (part.type === "input_text") return part.text;
if (part.type === "output_text") return part.text;
return "";
}).filter(Boolean).join("\n");
}
function buildAgentPrompt(input) {
if (typeof input === "string") return { message: input };
const systemParts = [];
const conversationEntries = [];
for (const item of input) if (item.type === "message") {
const content = extractTextContent(item.content).trim();
if (!content) continue;
if (item.role === "system" || item.role === "developer") {
systemParts.push(content);
continue;
}
const normalizedRole = item.role === "assistant" ? "assistant" : "user";
const sender = normalizedRole === "assistant" ? "Assistant" : "User";
conversationEntries.push({
role: normalizedRole,
entry: {
sender,
body: content
}
});
} else if (item.type === "function_call_output") conversationEntries.push({
role: "tool",
entry: {
sender: `Tool:${item.call_id}`,
body: item.output
}
});
return {
message: buildAgentMessageFromConversationEntries(conversationEntries),
extraSystemPrompt: systemParts.length > 0 ? systemParts.join("\n\n") : void 0
};
}
//#endregion
//#region src/gateway/openresponses-http.ts
/**
* OpenResponses HTTP Handler
*
* Implements the OpenResponses `/v1/responses` endpoint for OpenClaw Gateway.
*
* @see https://www.open-responses.com/
*/
const DEFAULT_BODY_BYTES$1 = 20 * 1024 * 1024;
const DEFAULT_MAX_URL_PARTS = 8;
const RESPONSE_SESSION_TTL_MS = 1800 * 1e3;
const MAX_RESPONSE_SESSION_ENTRIES = 500;
const responseSessionMap = /* @__PURE__ */ new Map();
function normalizeResponseSessionScope(scope) {
const user = scope.user?.trim();
const requestedSessionKey = scope.requestedSessionKey?.trim();
return {
agentId: scope.agentId,
user: user || void 0,
requestedSessionKey: requestedSessionKey || void 0
};
}
function createResponseSessionScope(params) {
return normalizeResponseSessionScope({
agentId: params.agentId,
user: params.user,
requestedSessionKey: getHeader(params.req, "x-openclaw-session-key")
});
}
function matchesResponseSessionScope(entry, scope) {
return entry.agentId === scope.agentId && entry.user === scope.user && entry.requestedSessionKey === scope.requestedSessionKey;
}
function pruneExpiredResponseSessions(now) {
while (responseSessionMap.size > 0) {
const oldest = responseSessionMap.entries().next().value;
if (!oldest) return;
const [oldestKey, oldestValue] = oldest;
if (now - oldestValue.ts <= RESPONSE_SESSION_TTL_MS) return;
responseSessionMap.delete(oldestKey);
}
}
function evictOverflowResponseSessions() {
while (responseSessionMap.size > MAX_RESPONSE_SESSION_ENTRIES) {
const oldestKey = responseSessionMap.keys().next().value;
if (!oldestKey) return;
responseSessionMap.delete(oldestKey);
}
}
function storeResponseSession(responseId, sessionKey, scope, now = Date.now()) {
responseSessionMap.delete(responseId);
responseSessionMap.set(responseId, {
...scope,
sessionKey,
ts: now
});
pruneExpiredResponseSessions(now);
evictOverflowResponseSessions();
}
function lookupResponseSession(responseId, scope, now = Date.now()) {
if (!responseId) return;
const entry = responseSessionMap.get(responseId);
if (!entry) return;
if (now - entry.ts > RESPONSE_SESSION_TTL_MS) {
responseSessionMap.delete(responseId);
return;
}
if (!matchesResponseSessionScope(entry, scope)) return;
return entry.sessionKey;
}
function writeSseEvent(res, event) {
res.write(`event: ${event.type}\n`);
res.write(`data: ${JSON.stringify(event)}\n\n`);
}
function resolveResponsesLimits(config) {
const files = config?.files;
const images = config?.images;
const fileLimits = resolveInputFileLimits(files);
return {
maxBodyBytes: config?.maxBodyBytes ?? DEFAULT_BODY_BYTES$1,
maxUrlParts: typeof config?.maxUrlParts === "number" ? Math.max(0, Math.floor(config.maxUrlParts)) : DEFAULT_MAX_URL_PARTS,
files: {
...fileLimits,
urlAllowlist: normalizeInputHostnameAllowlist(files?.urlAllowlist)
},
images: {
allowUrl: images?.allowUrl ?? true,
urlAllowlist: normalizeInputHostnameAllowlist(images?.urlAllowlist),
allowedMimes: normalizeMimeList(images?.allowedMimes, DEFAULT_INPUT_IMAGE_MIMES),
maxBytes: images?.maxBytes ?? 10485760,
maxRedirects: images?.maxRedirects ?? 3,
timeoutMs: images?.timeoutMs ?? 1e4
}
};
}
function extractClientTools(body) {
return body.tools ?? [];
}
function applyToolChoice(params) {
const { tools, toolChoice } = params;
if (!toolChoice) return { tools };
if (toolChoice === "none") return { tools: [] };
if (toolChoice === "required") {
if (tools.length === 0) throw new Error("tool_choice=required but no tools were provided");
return {
tools,
extraSystemPrompt: "You must call one of the available tools before responding."
};
}
if (typeof toolChoice === "object" && toolChoice.type === "function") {
const targetName = toolChoice.function?.name?.trim();
if (!targetName) throw new Error("tool_choice.function.name is required");
const matched = tools.filter((tool) => tool.function?.name === targetName);
if (matched.length === 0) throw new Error(`tool_choice requested unknown tool: ${targetName}`);
return {
tools: matched,
extraSystemPrompt: `You must call the ${targetName} tool before responding.`
};
}
return { tools };
}
function createEmptyUsage() {
return {
input_tokens: 0,
output_tokens: 0,
total_tokens: 0
};
}
function toUsage(value) {
if (!value) return createEmptyUsage();
const input = value.input ?? 0;
const output = value.output ?? 0;
const cacheRead = value.cacheRead ?? 0;
const cacheWrite = value.cacheWrite ?? 0;
const total = value.total ?? input + output + cacheRead + cacheWrite;
return {
input_tokens: Math.max(0, input),
output_tokens: Math.max(0, output),
total_tokens: Math.max(0, total)
};
}
function extractUsageFromResult(result) {
const meta = result?.meta;
return toUsage(meta && typeof meta === "object" ? meta.agentMeta?.usage : void 0);
}
function resolveStopReasonAndPendingToolCalls(meta) {
if (!meta || typeof meta !== "object") return {
stopReason: void 0,
pendingToolCalls: void 0
};
const record = meta;
return {
stopReason: record.stopReason,
pendingToolCalls: record.pendingToolCalls
};
}
function createResponseResource(params) {
return {
id: params.id,
object: "response",
created_at: Math.floor(Date.now() / 1e3),
status: params.status,
model: params.model,
output: params.output,
usage: params.usage ?? createEmptyUsage(),
error: params.error
};
}
function createAssistantOutputItem(params) {
return {
type: "message",
id: params.id,
role: "assistant",
content: [{
type: "output_text",
text: params.text
}],
status: params.status
};
}
function createFunctionCallOutputItem(params) {
return {
type: "function_call",
id: params.id,
call_id: params.callId,
name: params.name,
arguments: params.arguments,
status: params.status
};
}
async function runResponsesAgentCommand(params) {
return agentCommandFromIngress({
message: params.message,
images: params.images.length > 0 ? params.images : void 0,
clientTools: params.clientTools.length > 0 ? params.clientTools : void 0,
extraSystemPrompt: params.extraSystemPrompt || void 0,
streamParams: params.streamParams ?? void 0,
sessionKey: params.sessionKey,
runId: params.runId,
deliver: false,
messageChannel: params.messageChannel,
bestEffortDeliver: false,
senderIsOwner: true,
allowModelOverride: true
}, defaultRuntime, params.deps);
}
async function handleOpenResponsesHttpRequest(req, res, opts) {
const limits = resolveResponsesLimits(opts.config);
const maxBodyBytes = opts.maxBodyBytes ?? (opts.config?.maxBodyBytes ? limits.maxBodyBytes : Math.max(limits.maxBodyBytes, limits.files.maxBytes * 2, limits.images.maxBytes * 2));
const handled = await handleGatewayPostJsonEndpoint(req, res, {
pathname: "/v1/responses",
auth: opts.auth,
trustedProxies: opts.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback,
rateLimiter: opts.rateLimiter,
maxBodyBytes
});
if (handled === false) return false;
if (!handled) return true;
const parseResult = CreateResponseBodySchema.safeParse(handled.body);
if (!parseResult.success) {
const issue = parseResult.error.issues[0];
sendJson$1(res, 400, { error: {
message: issue ? `${issue.path.join(".")}: ${issue.message}` : "Invalid request body",
type: "invalid_request_error"
} });
return true;
}
const payload = parseResult.data;
const stream = Boolean(payload.stream);
const model = payload.model;
const user = payload.user;
let images = [];
let fileContexts = [];
let urlParts = 0;
const markUrlPart = () => {
urlParts += 1;
if (urlParts > limits.maxUrlParts) throw new Error(`Too many URL-based input sources: ${urlParts} (limit: ${limits.maxUrlParts})`);
};
try {
if (Array.isArray(payload.input)) {
for (const item of payload.input) if (item.type === "message" && typeof item.content !== "string") for (const part of item.content) {
if (part.type === "input_image") {
const source = part.source;
const sourceType = source.type === "base64" || source.type === "url" ? source.type : void 0;
if (!sourceType) throw new Error("input_image must have 'source.url' or 'source.data'");
if (sourceType === "url") markUrlPart();
const image = await extractImageContentFromSource(sourceType === "url" ? {
type: "url",
url: source.url ?? "",
mediaType: source.media_type
} : {
type: "base64",
data: source.data ?? "",
mediaType: source.media_type
}, limits.images);
images.push(image);
continue;
}
if (part.type === "input_file") {
const source = part.source;
const sourceType = source.type === "base64" || source.type === "url" ? source.type : void 0;
if (!sourceType) throw new Error("input_file must have 'source.url' or 'source.data'");
if (sourceType === "url") markUrlPart();
const file = await extractFileContentFromSource({
source: sourceType === "url" ? {
type: "url",
url: source.url ?? "",
mediaType: source.media_type,
filename: source.filename
} : {
type: "base64",
data: source.data ?? "",
mediaType: source.media_type,
filename: source.filename
},
limits: limits.files
});
if (file.text?.trim()) fileContexts.push(renderFileContextBlock({
filename: file.filename,
content: file.text
}));
else if (file.images && file.images.length > 0) fileContexts.push(renderFileContextBlock({
filename: file.filename,
content: "[PDF content rendered to images]",
surroundContentWithNewlines: false
}));
if (file.images && file.images.length > 0) images = images.concat(file.images);
}
}
}
} catch (err) {
logWarn(`openresponses: request parsing failed: ${String(err)}`);
sendJson$1(res, 400, { error: {
message: "invalid request",
type: "invalid_request_error"
} });
return true;
}
const clientTools = extractClientTools(payload);
let toolChoicePrompt;
let resolvedClientTools = clientTools;
try {
const toolChoiceResult = applyToolChoice({
tools: clientTools,
toolChoice: payload.tool_choice
});
resolvedClientTools = toolChoiceResult.tools;
toolChoicePrompt = toolChoiceResult.extraSystemPrompt;
} catch (err) {
logWarn(`openresponses: tool configuration failed: ${String(err)}`);
sendJson$1(res, 400, { error: {
message: "invalid tool configuration",
type: "invalid_request_error"
} });
return true;
}
const resolved = resolveGatewayRequestContext({
req,
model,
user,
sessionPrefix: "openresponses",
defaultMessageChannel: "webchat",
useMessageChannelHeader: false
});
const responseSessionScope = createResponseSessionScope({
req,
agentId: resolved.agentId,
user
});
const sessionKey = lookupResponseSession(payload.previous_response_id, responseSessionScope) ?? resolved.sessionKey;
const messageChannel = resolved.messageChannel;
const prompt = buildAgentPrompt(payload.input);
const fileContext = fileContexts.length > 0 ? fileContexts.join("\n\n") : void 0;
const toolChoiceContext = toolChoicePrompt?.trim();
const extraSystemPrompt = [
payload.instructions,
prompt.extraSystemPrompt,
toolChoiceContext,
fileContext
].filter(Boolean).join("\n\n");
if (!prompt.message) {
sendJson$1(res, 400, { error: {
message: "Missing user message in `input`.",
type: "invalid_request_error"
} });
return true;
}
const responseId = `resp_${randomUUID()}`;
const rememberResponseSession = () => storeResponseSession(responseId, sessionKey, responseSessionScope);
const outputItemId = `msg_${randomUUID()}`;
const deps = createDefaultDeps();
const streamParams = typeof payload.max_output_tokens === "number" ? { maxTokens: payload.max_output_tokens } : void 0;
if (!stream) {
try {
const result = await runResponsesAgentCommand({
message: prompt.message,
images,
clientTools: resolvedClientTools,
extraSystemPrompt,
streamParams,
sessionKey,
runId: responseId,
messageChannel,
deps
});
const payloads = result?.payloads;
const usage = extractUsageFromResult(result);
const meta = result?.meta;
const { stopReason, pendingToolCalls } = resolveStopReasonAndPendingToolCalls(meta);
if (stopReason === "tool_calls" && pendingToolCalls && pendingToolCalls.length > 0) {
const functionCall = pendingToolCalls[0];
const functionCallItemId = `call_${randomUUID()}`;
const assistantText = Array.isArray(payloads) && payloads.length > 0 ? payloads.map((p) => typeof p.text === "string" ? p.text : "").filter(Boolean).join("\n\n") : "";
const output = [];
if (assistantText) output.push(createAssistantOutputItem({
id: outputItemId,
text: assistantText,
status: "completed"
}));
output.push(createFunctionCallOutputItem({
id: functionCallItemId,
callId: functionCall.id,
name: functionCall.name,
arguments: functionCall.arguments
}));
const response = createResponseResource({
id: responseId,
model,
status: "incomplete",
output,
usage
});
rememberResponseSession();
sendJson$1(res, 200, response);
return true;
}
const response = createResponseResource({
id: responseId,
model,
status: "completed",
output: [createAssistantOutputItem({
id: outputItemId,
text: Array.isArray(payloads) && payloads.length > 0 ? payloads.map((p) => typeof p.text === "string" ? p.text : "").filter(Boolean).join("\n\n") : "No response from OpenClaw.",
status: "completed"
})],
usage
});
rememberResponseSession();
sendJson$1(res, 200, response);
} catch (err) {
logWarn(`openresponses: non-stream response failed: ${String(err)}`);
const response = createResponseResource({
id: responseId,
model,
status: "failed",
output: [],
error: {
code: "api_error",
message: "internal error"
}
});
rememberResponseSession();
sendJson$1(res, 500, response);
}
return true;
}
setSseHeaders(res);
let accumulatedText = "";
let sawAssistantDelta = false;
let closed = false;
let unsubscribe = () => {};
let finalUsage;
let finalizeRequested = null;
const maybeFinalize = () => {
if (closed) return;
if (!finalizeRequested) return;
if (!finalUsage) return;
const usage = finalUsage;
closed = true;
unsubscribe();
writeSseEvent(res, {
type: "response.output_text.done",
item_id: outputItemId,
output_index: 0,
content_index: 0,
text: finalizeRequested.text
});
writeSseEvent(res, {
type: "response.content_part.done",
item_id: outputItemId,
output_index: 0,
content_index: 0,
part: {
type: "output_text",
text: finalizeRequested.text
}
});
const completedItem = createAssistantOutputItem({
id: outputItemId,
text: finalizeRequested.text,
status: "completed"
});
writeSseEvent(res, {
type: "response.output_item.done",
output_index: 0,
item: completedItem
});
const finalResponse = createResponseResource({
id: responseId,
model,
status: finalizeRequested.status,
output: [completedItem],
usage
});
rememberResponseSession();
writeSseEvent(res, {
type: "response.completed",
response: finalResponse
});
writeDone(res);
res.end();
};
const requestFinalize = (status, text) => {
if (finalizeRequested) return;
finalizeRequested = {
status,
text
};
maybeFinalize();
};
const initialResponse = createResponseResource({
id: responseId,
model,
status: "in_progress",
output: []
});
writeSseEvent(res, {
type: "response.created",
response: initialResponse
});
writeSseEvent(res, {
type: "response.in_progress",
response: initialResponse
});
writeSseEvent(res, {
type: "response.output_item.added",
output_index: 0,
item: createAssistantOutputItem({
id: outputItemId,
text: "",
status: "in_progress"
})
});
writeSseEvent(res, {
type: "response.content_part.added",
item_id: outputItemId,
output_index: 0,
content_index: 0,
part: {
type: "output_text",
text: ""
}
});
unsubscribe = onAgentEvent((evt) => {
if (evt.runId !== responseId) return;
if (closed) return;
if (evt.stream === "assistant") {
const content = resolveAssistantStreamDeltaText(evt);
if (!content) return;
sawAssistantDelta = true;
accumulatedText += content;
writeSseEvent(res, {
type: "response.output_text.delta",
item_id: outputItemId,
output_index: 0,
content_index: 0,
delta: content
});
return;
}
if (evt.stream === "lifecycle") {
const phase = evt.data?.phase;
if (phase === "end" || phase === "error") requestFinalize(phase === "error" ? "failed" : "completed", accumulatedText || "No response from OpenClaw.");
}
});
req.on("close", () => {
closed = true;
unsubscribe();
});
(async () => {
try {
const result = await runResponsesAgentCommand({
message: prompt.message,
images,
clientTools: resolvedClientTools,
extraSystemPrompt,
streamParams,
sessionKey,
runId: responseId,
messageChannel,
deps
});
finalUsage = extractUsageFromResult(result);
const resultAny = result;
const meta = resultAny.meta;
const { stopReason, pendingToolCalls } = resolveStopReasonAndPendingToolCalls(meta);
if (!closed && stopReason === "tool_calls" && pendingToolCalls && pendingToolCalls.length > 0) {
const functionCall = pendingToolCalls[0];
const usage = finalUsage ?? createEmptyUsage();
const finalText = accumulatedText || (Array.isArray(resultAny.payloads) ? resultAny.payloads.map((p) => typeof p.text === "string" ? p.text : "").filter(Boolean).join("\n\n") : "");
writeSseEvent(res, {
type: "response.output_text.done",
item_id: outputItemId,
output_index: 0,
content_index: 0,
text: finalText
});
writeSseEvent(res, {
type: "response.content_part.done",
item_id: outputItemId,
output_index: 0,
content_index: 0,
part: {
type: "output_text",
text: finalText
}
});
const completedItem = createAssistantOutputItem({
id: outputItemId,
text: finalText,
status: "completed"
});
writeSseEvent(res, {
type: "response.output_item.done",
output_index: 0,
item: completedItem
});
const functionCallItemId = `call_${randomUUID()}`;
const functionCallItem = createFunctionCallOutputItem({
id: functionCallItemId,
callId: functionCall.id,
name: functionCall.name,
arguments: functionCall.arguments
});
writeSseEvent(res, {
type: "response.output_item.added",
output_index: 1,
item: functionCallItem
});
writeSseEvent(res, {
type: "response.output_item.done",
output_index: 1,
item: createFunctionCallOutputItem({
id: functionCallItemId,
callId: functionCall.id,
name: functionCall.name,
arguments: functionCall.arguments,
status: "completed"
})
});
const incompleteResponse = createResponseResource({
id: responseId,
model,
status: "incomplete",
output: [completedItem, functionCallItem],
usage
});
closed = true;
unsubscribe();
rememberResponseSession();
writeSseEvent(res, {
type: "response.completed",
response: incompleteResponse
});
writeDone(res);
res.end();
return;
}
maybeFinalize();
if (closed) return;
if (!sawAssistantDelta) {
const payloads = resultAny.payloads;
const content = Array.isArray(payloads) && payloads.length > 0 ? payloads.map((p) => typeof p.text === "string" ? p.text : "").filter(Boolean).join("\n\n") : "No response from OpenClaw.";
accumulatedText = content;
sawAssistantDelta = true;
writeSseEvent(res, {
type: "response.output_text.delta",
item_id: outputItemId,
output_index: 0,
content_index: 0,
delta: content
});
}
} catch (err) {
logWarn(`openresponses: streaming response failed: ${String(err)}`);
if (closed) return;
finalUsage = finalUsage ?? createEmptyUsage();
const errorResponse = createResponseResource({
id: responseId,
model,
status: "failed",
output: [],
error: {
code: "api_error",
message: "internal error"
},
usage: finalUsage
});
rememberResponseSession();
writeSseEvent(res, {
type: "response.failed",
response: errorResponse
});
emitAgentEvent({
runId: responseId,
stream: "lifecycle",
data: { phase: "error" }
});
} finally {
if (!closed) emitAgentEvent({
runId: responseId,
stream: "lifecycle",
data: { phase: "end" }
});
}
})();
return true;
}
//#endregion
//#region src/gateway/server/http-auth.ts
function isCanvasPath(pathname) {
return pathname === "/__openclaw__/a2ui" || pathname.startsWith(`/__openclaw__/a2ui/`) || pathname === "/__openclaw__/canvas" || pathname.startsWith(`/__openclaw__/canvas/`) || pathname === "/__openclaw__/ws";
}
function isNodeWsClient(client) {
if (client.connect.role === "node") return true;
return normalizeGatewayClientMode(client.connect.client.mode) === GATEWAY_CLIENT_MODES.NODE;
}
function hasAuthorizedNodeWsClientForCanvasCapability(clients, capability) {
const nowMs = Date.now();
for (const client of clients) {
if (!isNodeWsClient(client)) continue;
if (!client.canvasCapability || !client.canvasCapabilityExpiresAtMs) continue;
if (client.canvasCapabilityExpiresAtMs <= nowMs) continue;
if (safeEqualSecret(client.canvasCapability, capability)) {
client.canvasCapabilityExpiresAtMs = nowMs + CANVAS_CAPABILITY_TTL_MS;
return true;
}
}
return false;
}
async function authorizeCanvasRequest(params) {
const { req, auth, trustedProxies, allowRealIpFallback, clients, canvasCapability, malformedScopedPath, rateLimiter } = params;
if (malformedScopedPath) return {
ok: false,
reason: "unauthorized"
};
let lastAuthFailure = null;
const token = getBearerToken(req);
if (token) {
const authResult = await authorizeHttpGatewayConnect({
auth: {
...auth,
allowTailscale: false
},
connectAuth: {
token,
password: token
},
req,
trustedProxies,
allowRealIpFallback,
rateLimiter
});
if (authResult.ok) return authResult;
lastAuthFailure = authResult;
}
if (canvasCapability && hasAuthorizedNodeWsClientForCanvasCapability(clients, canvasCapability)) return { ok: true };
return lastAuthFailure ?? {
ok: false,
reason: "unauthorized"
};
}
async function enforcePluginRouteGatewayAuth(params) {
return await authorizeGatewayBearerRequestOrReply(params);
}
//#endregion
//#region src/gateway/server/plugins-http/path-context.ts
function normalizeProtectedPrefix(prefix) {
const collapsed = prefix.toLowerCase().replace(/\/{2,}/g, "/");
if (collapsed.length <= 1) return collapsed || "/";
return collapsed.replace(/\/+$/, "");
}
function prefixMatchPath(pathname, prefix) {
return pathname === prefix || pathname.startsWith(`${prefix}/`) || pathname.startsWith(`${prefix}%`);
}
const NORMALIZED_PROTECTED_PLUGIN_ROUTE_PREFIXES = PROTECTED_PLUGIN_ROUTE_PREFIXES.map(normalizeProtectedPrefix);
function isProtectedPluginRoutePathFromContext(context) {
if (context.candidates.some((candidate) => NORMALIZED_PROTECTED_PLUGIN_ROUTE_PREFIXES.some((prefix) => prefixMatchPath(candidate, prefix)))) return true;
if (!context.malformedEncoding) return false;
return NORMALIZED_PROTECTED_PLUGIN_ROUTE_PREFIXES.some((prefix) => prefixMatchPath(context.rawNormalizedPath, prefix));
}
function resolvePluginRoutePathContext(pathname) {
const canonical = canonicalizePathForSecurity(pathname);
return {
pathname,
canonicalPath: canonical.canonicalPath,
candidates: canonical.candidates,
malformedEncoding: canonical.malformedEncoding,
decodePassLimitReached: canonical.decodePassLimitReached,
rawNormalizedPath: canonical.rawNormalizedPath
};
}
//#endregion
//#region src/gateway/server/plugins-http/route-match.ts
function doesPluginRouteMatchPath(route, context) {
const routeCanonicalPath = canonicalizePathVariant(route.path);
if (route.match === "prefix") return context.candidates.some((candidate) => prefixMatchPath(candidate, routeCanonicalPath));
return context.candidates.some((candidate) => candidate === routeCanonicalPath);
}
function findMatchingPluginHttpRoutes(registry, context) {
const routes = registry.httpRoutes ?? [];
if (routes.length === 0) return [];
const exactMatches = [];
const prefixMatches = [];
for (const route of routes) {
if (!doesPluginRouteMatchPath(route, context)) continue;
if (route.match === "prefix") prefixMatches.push(route);
else exactMatches.push(route);
}
exactMatches.sort((a, b) => b.path.length - a.path.length);
prefixMatches.sort((a, b) => b.path.length - a.path.length);
return [...exactMatches, ...prefixMatches];
}
//#endregion
//#region src/gateway/server/plugins-http/route-auth.ts
function matchedPluginRoutesRequireGatewayAuth(routes) {
return routes.some((route) => route.auth === "gateway");
}
function shouldEnforceGatewayAuthForPluginPath(registry, pathnameOrContext) {
const pathContext = typeof pathnameOrContext === "string" ? resolvePluginRoutePathContext(pathnameOrContext) : pathnameOrContext;
if (pathContext.malformedEncoding || pathContext.decodePassLimitReached) return true;
if (isProtectedPluginRoutePathFromContext(pathContext)) return true;
return matchedPluginRoutesRequireGatewayAuth(findMatchingPluginHttpRoutes(registry, pathContext));
}
//#endregion
//#region src/gateway/server/plugins-http.ts
function createPluginRouteRuntimeClient(params) {
const scopes = params.requiresGatewayAuth && params.gatewayAuthSatisfied !== false ? [
ADMIN_SCOPE$2,
APPROVALS_SCOPE,
PAIRING_SCOPE
] : [WRITE_SCOPE];
return { connect: {
minProtocol: 3,
maxProtocol: 3,
client: {
id: GATEWAY_CLIENT_IDS.GATEWAY_CLIENT,
version: "internal",
platform: "node",
mode: GATEWAY_CLIENT_MODES.BACKEND
},
role: "operator",
scopes
} };
}
function createGatewayPluginRequestHandler(params) {
const { log } = params;
return async (req, res, providedPathContext, dispatchContext) => {
const registry = resolveActivePluginHttpRouteRegistry(params.registry);
if ((registry.httpRoutes ?? []).length === 0) return false;
const pathContext = providedPathContext ?? resolvePluginRoutePathContext(new URL(req.url ?? "/", "http://localhost").pathname);
const matchedRoutes = findMatchingPluginHttpRoutes(registry, pathContext);
if (matchedRoutes.length === 0) return false;
const requiresGatewayAuth = matchedPluginRoutesRequireGatewayAuth(matchedRoutes);
if (requiresGatewayAuth && dispatchContext?.gatewayAuthSatisfied === false) {
log.warn(`plugin http route blocked without gateway auth (${pathContext.canonicalPath})`);
return false;
}
return await withPluginRuntimeGatewayRequestScope({
client: createPluginRouteRuntimeClient({
requiresGatewayAuth,
gatewayAuthSatisfied: dispatchContext?.gatewayAuthSatisfied
}),
isWebchatConnect: () => false
}, async () => {
for (const route of matchedRoutes) try {
if (await route.handler(req, res) !== false) return true;
} catch (err) {
log.warn(`plugin http route failed (${route.pluginId ?? "unknown"}): ${String(err)}`);
if (!res.headersSent) {
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Internal Server Error");
}
return true;
}
return false;
});
};
}
//#endregion
//#region src/gateway/session-kill-http.ts
const REQUESTER_SESSION_KEY_HEADER = "x-openclaw-requester-session-key";
function canBearerTokenKillSessions(token, authOk) {
if (!token || !authOk) return false;
const bearerScopes = [ADMIN_SCOPE$2, WRITE_SCOPE];
return authorizeOperatorScopesForMethod("sessions.delete", bearerScopes).allowed || authorizeOperatorScopesForMethod("sessions.abort", bearerScopes).allowed;
}
function resolveSessionKeyFromPath(pathname) {
const match = pathname.match(/^\/sessions\/([^/]+)\/kill$/);
if (!match) return null;
try {
return decodeURIComponent(match[1] ?? "").trim() || null;
} catch {
return null;
}
}
async function handleSessionKillHttpRequest(req, res, opts) {
const cfg = loadConfig();
const sessionKey = resolveSessionKeyFromPath(new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`).pathname);
if (!sessionKey) return false;
if (req.method !== "POST") {
sendMethodNotAllowed(res, "POST");
return true;
}
const token = getBearerToken(req);
const authResult = await authorizeHttpGatewayConnect({
auth: opts.auth,
connectAuth: token ? {
token,
password: token
} : null,
req,
trustedProxies: opts.trustedProxies ?? cfg.gateway?.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback ?? cfg.gateway?.allowRealIpFallback,
rateLimiter: opts.rateLimiter
});
if (!authResult.ok) {
sendGatewayAuthFailure(res, authResult);
return true;
}
const { entry, canonicalKey } = loadSessionEntry(sessionKey);
if (!entry) {
sendJson$1(res, 404, {
ok: false,
error: {
type: "not_found",
message: `Session not found: ${sessionKey}`
}
});
return true;
}
const trustedProxies = opts.trustedProxies ?? cfg.gateway?.trustedProxies;
const allowRealIpFallback = opts.allowRealIpFallback ?? cfg.gateway?.allowRealIpFallback;
const requesterSessionKey = req.headers[REQUESTER_SESSION_KEY_HEADER]?.toString().trim();
const allowLocalAdminKill = isLocalDirectRequest(req, trustedProxies, allowRealIpFallback);
const allowBearerOperatorKill = canBearerTokenKillSessions(token, authResult.ok);
if (!requesterSessionKey && !allowLocalAdminKill && !allowBearerOperatorKill) {
sendJson$1(res, 403, {
ok: false,
error: {
type: "forbidden",
message: "Session kills require a local admin request, requester session ownership, or an authorized operator token."
}
});
return true;
}
const allowAdminKill = allowLocalAdminKill || allowBearerOperatorKill;
let killed = false;
if (!allowAdminKill && requesterSessionKey) {
const runEntry = getSubagentRunByChildSessionKey(canonicalKey);
if (runEntry) {
const result = await killControlledSubagentRun({
cfg,
controller: resolveSubagentController({
cfg,
agentSessionKey: requesterSessionKey
}),
entry: runEntry
});
if (result.status === "forbidden") {
sendJson$1(res, 403, {
ok: false,
error: {
type: "forbidden",
message: result.error
}
});
return true;
}
killed = result.status === "ok";
}
} else killed = (await killSubagentRunAdmin({
cfg,
sessionKey: canonicalKey
})).killed;
sendJson$1(res, 200, {
ok: true,
killed
});
return true;
}
//#endregion
//#region src/gateway/sessions-history-http.ts
const MAX_SESSION_HISTORY_LIMIT = 1e3;
function resolveSessionHistoryPath(req) {
const match = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`).pathname.match(/^\/sessions\/([^/]+)\/history$/);
if (!match) return null;
try {
return decodeURIComponent(match[1] ?? "").trim() || null;
} catch {
return "";
}
}
function shouldStreamSse(req) {
return (getHeader(req, "accept")?.toLowerCase() ?? "").includes("text/event-stream");
}
function getRequestUrl(req) {
return new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`);
}
function resolveLimit(req) {
const raw = getRequestUrl(req).searchParams.get("limit");
if (raw == null || raw.trim() === "") return;
const value = Number.parseInt(raw, 10);
if (!Number.isFinite(value) || value < 1) return 1;
return Math.min(MAX_SESSION_HISTORY_LIMIT, Math.max(1, value));
}
function resolveCursor(req) {
const trimmed = getRequestUrl(req).searchParams.get("cursor")?.trim();
return trimmed ? trimmed : void 0;
}
function resolveCursorSeq(cursor) {
if (!cursor) return;
const normalized = cursor.startsWith("seq:") ? cursor.slice(4) : cursor;
const value = Number.parseInt(normalized, 10);
return Number.isFinite(value) && value > 0 ? value : void 0;
}
function resolveMessageSeq(message) {
if (!message || typeof message !== "object" || Array.isArray(message)) return;
const meta = message.__openclaw;
if (!meta || typeof meta !== "object" || Array.isArray(meta)) return;
const seq = meta.seq;
return typeof seq === "number" && Number.isFinite(seq) && seq > 0 ? seq : void 0;
}
function paginateSessionMessages(messages, limit, cursor) {
const cursorSeq = resolveCursorSeq(cursor);
const endExclusive = typeof cursorSeq === "number" ? Math.max(0, Math.min(messages.length, cursorSeq - 1)) : messages.length;
const start = typeof limit === "number" && limit > 0 ? Math.max(0, endExclusive - limit) : 0;
const items = messages.slice(start, endExclusive);
const firstSeq = resolveMessageSeq(items[0]);
return {
items,
messages: items,
hasMore: start > 0,
...start > 0 && typeof firstSeq === "number" ? { nextCursor: String(firstSeq) } : {}
};
}
function canonicalizePath(value) {
const trimmed = value?.trim();
if (!trimmed) return;
const resolved = path.resolve(trimmed);
try {
return fs.realpathSync(resolved);
} catch {
return resolved;
}
}
function sseWrite(res, event, payload) {
res.write(`event: ${event}\n`);
res.write(`data: ${JSON.stringify(payload)}\n\n`);
}
async function handleSessionHistoryHttpRequest(req, res, opts) {
const sessionKey = resolveSessionHistoryPath(req);
if (sessionKey === null) return false;
if (!sessionKey) {
sendInvalidRequest(res, "invalid session key");
return true;
}
if (req.method !== "GET") {
sendMethodNotAllowed(res, "GET");
return true;
}
const cfg = loadConfig();
const token = getBearerToken(req);
const authResult = await authorizeHttpGatewayConnect({
auth: opts.auth,
connectAuth: token ? {
token,
password: token
} : null,
req,
trustedProxies: opts.trustedProxies ?? cfg.gateway?.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback ?? cfg.gateway?.allowRealIpFallback,
rateLimiter: opts.rateLimiter
});
if (!authResult.ok) {
sendGatewayAuthFailure(res, authResult);
return true;
}
const target = resolveGatewaySessionStoreTarget({
cfg,
key: sessionKey
});
const store = loadSessionStore(target.storePath);
const entry = target.storeKeys.map((key) => store[key]).find(Boolean);
if (!entry?.sessionId) {
sendJson$1(res, 404, {
ok: false,
error: {
type: "not_found",
message: `Session not found: ${sessionKey}`
}
});
return true;
}
const limit = resolveLimit(req);
const cursor = resolveCursor(req);
const history = paginateSessionMessages(entry?.sessionId ? readSessionMessages(entry.sessionId, target.storePath, entry.sessionFile) : [], limit, cursor);
if (!shouldStreamSse(req)) {
sendJson$1(res, 200, {
sessionKey: target.canonicalKey,
...history
});
return true;
}
const transcriptCandidates = entry?.sessionId ? new Set(resolveSessionTranscriptCandidates(entry.sessionId, target.storePath, entry.sessionFile, target.agentId).map((candidate) => canonicalizePath(candidate)).filter((candidate) => typeof candidate === "string")) : /* @__PURE__ */ new Set();
let sentHistory = history;
setSseHeaders(res);
res.write("retry: 1000\n\n");
sseWrite(res, "history", {
sessionKey: target.canonicalKey,
...sentHistory
});
const heartbeat = setInterval(() => {
if (!res.writableEnded) res.write(": keepalive\n\n");
}, 15e3);
const unsubscribe = onSessionTranscriptUpdate((update) => {
if (res.writableEnded || !entry?.sessionId) return;
const updatePath = canonicalizePath(update.sessionFile);
if (!updatePath || !transcriptCandidates.has(updatePath)) return;
if (update.message !== void 0) {
const previousSeq = resolveMessageSeq(sentHistory.items.at(-1));
const nextMessage = attachOpenClawTranscriptMeta(update.message, {
...typeof update.messageId === "string" ? { id: update.messageId } : {},
seq: typeof previousSeq === "number" ? previousSeq + 1 : readSessionMessages(entry.sessionId, target.storePath, entry.sessionFile).length
});
if (limit === void 0 && cursor === void 0) {
sentHistory = {
items: [...sentHistory.items, nextMessage],
messages: [...sentHistory.items, nextMessage],
hasMore: false
};
sseWrite(res, "message", {
sessionKey: target.canonicalKey,
message: nextMessage,
...typeof update.messageId === "string" ? { messageId: update.messageId } : {},
messageSeq: resolveMessageSeq(nextMessage)
});
return;
}
}
sentHistory = paginateSessionMessages(readSessionMessages(entry.sessionId, target.storePath, entry.sessionFile), limit, cursor);
sseWrite(res, "history", {
sessionKey: target.canonicalKey,
...sentHistory
});
});
const cleanup = () => {
clearInterval(heartbeat);
unsubscribe();
};
req.on("close", cleanup);
res.on("close", cleanup);
res.on("finish", cleanup);
return true;
}
//#endregion
//#region src/gateway/tools-invoke-http.ts
const DEFAULT_BODY_BYTES = 2 * 1024 * 1024;
const MEMORY_TOOL_NAMES = new Set(["memory_search", "memory_get"]);
function resolveSessionKeyFromBody(body) {
if (typeof body.sessionKey === "string" && body.sessionKey.trim()) return body.sessionKey.trim();
}
function resolveMemoryToolDisableReasons(cfg) {
if (!process.env.VITEST) return [];
const reasons = [];
const plugins = cfg.plugins;
const slotRaw = plugins?.slots?.memory;
const slotDisabled = slotRaw === null || typeof slotRaw === "string" && slotRaw.trim().toLowerCase() === "none";
const pluginsDisabled = plugins?.enabled === false;
const defaultDisabled = isTestDefaultMemorySlotDisabled(cfg);
if (pluginsDisabled) reasons.push("plugins.enabled=false");
if (slotDisabled) reasons.push(slotRaw === null ? "plugins.slots.memory=null" : "plugins.slots.memory=\"none\"");
if (!pluginsDisabled && !slotDisabled && defaultDisabled) reasons.push("memory plugin disabled by test default");
return reasons;
}
function mergeActionIntoArgsIfSupported(params) {
const { toolSchema, action, args } = params;
if (!action) return args;
if (args.action !== void 0) return args;
const schemaObj = toolSchema;
if (!Boolean(schemaObj && typeof schemaObj === "object" && schemaObj.properties && "action" in schemaObj.properties)) return args;
return {
...args,
action
};
}
function getErrorMessage(err) {
if (err instanceof Error) return err.message || String(err);
if (typeof err === "string") return err;
return String(err);
}
function resolveToolInputErrorStatus(err) {
if (err instanceof ToolInputError) {
const status = err.status;
return typeof status === "number" ? status : 400;
}
if (typeof err !== "object" || err === null || !("name" in err)) return null;
const name = err.name;
if (name !== "ToolInputError" && name !== "ToolAuthorizationError") return null;
const status = err.status;
if (typeof status === "number") return status;
return name === "ToolAuthorizationError" ? 403 : 400;
}
async function handleToolsInvokeHttpRequest(req, res, opts) {
if (new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`).pathname !== "/tools/invoke") return false;
if (req.method !== "POST") {
sendMethodNotAllowed(res, "POST");
return true;
}
const cfg = loadConfig();
const token = getBearerToken(req);
const authResult = await authorizeHttpGatewayConnect({
auth: opts.auth,
connectAuth: token ? {
token,
password: token
} : null,
req,
trustedProxies: opts.trustedProxies ?? cfg.gateway?.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback ?? cfg.gateway?.allowRealIpFallback,
rateLimiter: opts.rateLimiter
});
if (!authResult.ok) {
sendGatewayAuthFailure(res, authResult);
return true;
}
const bodyUnknown = await readJsonBodyOrError(req, res, opts.maxBodyBytes ?? DEFAULT_BODY_BYTES);
if (bodyUnknown === void 0) return true;
const body = bodyUnknown ?? {};
const toolName = typeof body.tool === "string" ? body.tool.trim() : "";
if (!toolName) {
sendInvalidRequest(res, "tools.invoke requires body.tool");
return true;
}
if (process.env.VITEST && MEMORY_TOOL_NAMES.has(toolName)) {
const reasons = resolveMemoryToolDisableReasons(cfg);
if (reasons.length > 0) {
sendJson$1(res, 400, {
ok: false,
error: {
type: "invalid_request",
message: `memory tools are disabled in tests${reasons.length > 0 ? ` (${reasons.join(", ")})` : ""}. Enable by setting plugins.slots.memory="memory-core" (and ensure plugins.enabled is not false).`
}
});
return true;
}
}
const action = typeof body.action === "string" ? body.action.trim() : void 0;
const argsRaw = body.args;
const args = argsRaw && typeof argsRaw === "object" && !Array.isArray(argsRaw) ? argsRaw : {};
const rawSessionKey = resolveSessionKeyFromBody(body);
const sessionKey = !rawSessionKey || rawSessionKey === "main" ? resolveMainSessionKey(cfg) : rawSessionKey;
const messageChannel = normalizeMessageChannel(getHeader(req, "x-openclaw-message-channel") ?? "");
const accountId = getHeader(req, "x-openclaw-account-id")?.trim() || void 0;
const agentTo = getHeader(req, "x-openclaw-message-to")?.trim() || void 0;
const agentThreadId = getHeader(req, "x-openclaw-thread-id")?.trim() || void 0;
const { agentId, globalPolicy, globalProviderPolicy, agentPolicy, agentProviderPolicy, profile, providerProfile, profileAlsoAllow, providerProfileAlsoAllow } = resolveEffectiveToolPolicy({
config: cfg,
sessionKey
});
const profilePolicy = resolveToolProfilePolicy(profile);
const providerProfilePolicy = resolveToolProfilePolicy(providerProfile);
const profilePolicyWithAlsoAllow = mergeAlsoAllowPolicy(profilePolicy, profileAlsoAllow);
const providerProfilePolicyWithAlsoAllow = mergeAlsoAllowPolicy(providerProfilePolicy, providerProfileAlsoAllow);
const groupPolicy = resolveGroupToolPolicy({
config: cfg,
sessionKey,
messageProvider: messageChannel ?? void 0,
accountId: accountId ?? null
});
const subagentPolicy = isSubagentSessionKey(sessionKey) ? resolveSubagentToolPolicy(cfg) : void 0;
const subagentFiltered = applyToolPolicyPipeline({
tools: createOpenClawTools({
agentSessionKey: sessionKey,
agentChannel: messageChannel ?? void 0,
agentAccountId: accountId,
agentTo,
agentThreadId,
allowGatewaySubagentBinding: true,
allowMediaInvokeCommands: true,
config: cfg,
pluginToolAllowlist: collectExplicitAllowlist([
profilePolicy,
providerProfilePolicy,
globalPolicy,
globalProviderPolicy,
agentPolicy,
agentProviderPolicy,
groupPolicy,
subagentPolicy
])
}),
toolMeta: (tool) => getPluginToolMeta(tool),
warn: logWarn,
steps: [...buildDefaultToolPolicyPipelineSteps({
profilePolicy: profilePolicyWithAlsoAllow,
profile,
providerProfilePolicy: providerProfilePolicyWithAlsoAllow,
providerProfile,
globalPolicy,
globalProviderPolicy,
agentPolicy,
agentProviderPolicy,
groupPolicy,
agentId
}), {
policy: subagentPolicy,
label: "subagent tools.allow"
}]
});
const gatewayToolsCfg = cfg.gateway?.tools;
const gatewayDenyNames = DEFAULT_GATEWAY_HTTP_TOOL_DENY.filter((name) => !gatewayToolsCfg?.allow?.includes(name)).concat(Array.isArray(gatewayToolsCfg?.deny) ? gatewayToolsCfg.deny : []);
const gatewayDenySet = new Set(gatewayDenyNames);
const tool = subagentFiltered.filter((t) => !gatewayDenySet.has(t.name)).find((t) => t.name === toolName);
if (!tool) {
sendJson$1(res, 404, {
ok: false,
error: {
type: "not_found",
message: `Tool not available: ${toolName}`
}
});
return true;
}
try {
const toolCallId = `http-${Date.now()}`;
const hookResult = await runBeforeToolCallHook({
toolName,
params: mergeActionIntoArgsIfSupported({
toolSchema: tool.parameters,
action,
args
}),
toolCallId,
ctx: {
agentId,
sessionKey,
loopDetection: resolveToolLoopDetectionConfig({
cfg,
agentId
})
}
});
if (hookResult.blocked) {
sendJson$1(res, 403, {
ok: false,
error: {
type: "tool_call_blocked",
message: hookResult.reason
}
});
return true;
}
sendJson$1(res, 200, {
ok: true,
result: await tool.execute?.(toolCallId, hookResult.params)
});
} catch (err) {
const inputStatus = resolveToolInputErrorStatus(err);
if (inputStatus !== null) {
sendJson$1(res, inputStatus, {
ok: false,
error: {
type: "tool_error",
message: getErrorMessage(err) || "invalid tool arguments"
}
});
return true;
}
logWarn(`tools-invoke: tool execution failed: ${String(err)}`);
sendJson$1(res, 500, {
ok: false,
error: {
type: "tool_error",
message: "tool execution failed"
}
});
}
return true;
}
//#endregion
//#region src/gateway/server-http.ts
const HOOK_AUTH_FAILURE_LIMIT = 20;
const HOOK_AUTH_FAILURE_WINDOW_MS = 6e4;
function resolveMappedHookExternalContentSource(params) {
const payloadSource = typeof params.payload.source === "string" ? params.payload.source.trim().toLowerCase() : "";
if (params.subPath === "gmail" || payloadSource === "gmail") return "gmail";
return resolveHookExternalContentSource(params.sessionKey) ?? "webhook";
}
function sendJson(res, status, body) {
res.statusCode = status;
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.end(JSON.stringify(body));
}
const GATEWAY_PROBE_STATUS_BY_PATH = new Map([
["/health", "live"],
["/healthz", "live"],
["/ready", "ready"],
["/readyz", "ready"]
]);
const MATTERMOST_SLASH_CALLBACK_PATH = "/api/channels/mattermost/command";
function resolveMattermostSlashCallbackPaths(configSnapshot) {
const callbackPaths = new Set([MATTERMOST_SLASH_CALLBACK_PATH]);
const isMattermostCommandCallbackPath = (path) => path === MATTERMOST_SLASH_CALLBACK_PATH || path.startsWith("/api/channels/mattermost/");
const normalizeCallbackPath = (value) => {
const trimmed = typeof value === "string" ? value.trim() : "";
if (!trimmed) return MATTERMOST_SLASH_CALLBACK_PATH;
return trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
};
const tryAddCallbackUrlPath = (rawUrl) => {
if (typeof rawUrl !== "string") return;
const trimmed = rawUrl.trim();
if (!trimmed) return;
try {
const pathname = new URL(trimmed).pathname;
if (pathname && isMattermostCommandCallbackPath(pathname)) callbackPaths.add(pathname);
} catch {}
};
const mmRaw = configSnapshot.channels?.mattermost;
const addMmCommands = (raw) => {
if (raw == null || typeof raw !== "object") return;
const commands = raw;
const callbackPath = normalizeCallbackPath(commands.callbackPath);
if (isMattermostCommandCallbackPath(callbackPath)) callbackPaths.add(callbackPath);
tryAddCallbackUrlPath(commands.callbackUrl);
};
addMmCommands(mmRaw?.commands);
const accountsRaw = mmRaw?.accounts ?? {};
for (const accountId of Object.keys(accountsRaw)) {
const accountCfg = accountsRaw[accountId];
addMmCommands(accountCfg?.commands);
}
return callbackPaths;
}
function shouldEnforceDefaultPluginGatewayAuth(pathContext) {
return pathContext.malformedEncoding || pathContext.decodePassLimitReached || isProtectedPluginRoutePathFromContext(pathContext);
}
async function canRevealReadinessDetails(params) {
if (isLocalDirectRequest(params.req, params.trustedProxies, params.allowRealIpFallback)) return true;
if (params.resolvedAuth.mode === "none") return false;
const bearerToken = getBearerToken(params.req);
return (await authorizeHttpGatewayConnect({
auth: params.resolvedAuth,
connectAuth: bearerToken ? {
token: bearerToken,
password: bearerToken
} : null,
req: params.req,
trustedProxies: params.trustedProxies,
allowRealIpFallback: params.allowRealIpFallback
})).ok;
}
async function handleGatewayProbeRequest(req, res, requestPath, resolvedAuth, trustedProxies, allowRealIpFallback, getReadiness) {
const status = GATEWAY_PROBE_STATUS_BY_PATH.get(requestPath);
if (!status) return false;
const method = (req.method ?? "GET").toUpperCase();
if (method !== "GET" && method !== "HEAD") {
res.statusCode = 405;
res.setHeader("Allow", "GET, HEAD");
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Method Not Allowed");
return true;
}
res.setHeader("Content-Type", "application/json; charset=utf-8");
res.setHeader("Cache-Control", "no-store");
let statusCode;
let body;
if (status === "ready" && getReadiness) {
const includeDetails = await canRevealReadinessDetails({
req,
resolvedAuth,
trustedProxies,
allowRealIpFallback
});
try {
const result = getReadiness();
statusCode = result.ready ? 200 : 503;
body = JSON.stringify(includeDetails ? result : { ready: result.ready });
} catch {
statusCode = 503;
body = JSON.stringify(includeDetails ? {
ready: false,
failing: ["internal"],
uptimeMs: 0
} : { ready: false });
}
} else {
statusCode = 200;
body = JSON.stringify({
ok: true,
status
});
}
res.statusCode = statusCode;
res.end(method === "HEAD" ? void 0 : body);
return true;
}
function writeUpgradeAuthFailure(socket, auth) {
if (auth.rateLimited) {
const retryAfterSeconds = auth.retryAfterMs && auth.retryAfterMs > 0 ? Math.ceil(auth.retryAfterMs / 1e3) : void 0;
socket.write([
"HTTP/1.1 429 Too Many Requests",
retryAfterSeconds ? `Retry-After: ${retryAfterSeconds}` : void 0,
"Content-Type: application/json; charset=utf-8",
"Connection: close",
"",
JSON.stringify({ error: {
message: "Too many failed authentication attempts. Please try again later.",
type: "rate_limited"
} })
].filter(Boolean).join("\r\n"));
return;
}
socket.write("HTTP/1.1 401 Unauthorized\r\nConnection: close\r\n\r\n");
}
async function runGatewayHttpRequestStages(stages) {
for (const stage of stages) if (await stage.run()) return true;
return false;
}
function buildPluginRequestStages(params) {
if (!params.handlePluginRequest) return [];
let pluginGatewayAuthSatisfied = false;
return [{
name: "plugin-auth",
run: async () => {
if (params.mattermostSlashCallbackPaths.has(params.requestPath)) return false;
const pathContext = params.pluginPathContext ?? resolvePluginRoutePathContext(params.requestPath);
if (!(params.shouldEnforcePluginGatewayAuth ?? shouldEnforceDefaultPluginGatewayAuth)(pathContext)) return false;
if (!await enforcePluginRouteGatewayAuth({
req: params.req,
res: params.res,
auth: params.resolvedAuth,
trustedProxies: params.trustedProxies,
allowRealIpFallback: params.allowRealIpFallback,
rateLimiter: params.rateLimiter
})) return true;
pluginGatewayAuthSatisfied = true;
return false;
}
}, {
name: "plugin-http",
run: () => {
const pathContext = params.pluginPathContext ?? resolvePluginRoutePathContext(params.requestPath);
return params.handlePluginRequest?.(params.req, params.res, pathContext, { gatewayAuthSatisfied: pluginGatewayAuthSatisfied }) ?? false;
}
}];
}
function createHooksRequestHandler(opts) {
const { getHooksConfig, logHooks, dispatchAgentHook, dispatchWakeHook, getClientIpConfig } = opts;
const hookReplayCache = /* @__PURE__ */ new Map();
const hookAuthLimiter = createAuthRateLimiter({
maxAttempts: HOOK_AUTH_FAILURE_LIMIT,
windowMs: HOOK_AUTH_FAILURE_WINDOW_MS,
lockoutMs: HOOK_AUTH_FAILURE_WINDOW_MS,
exemptLoopback: false,
pruneIntervalMs: 0
});
const resolveHookClientKey = (req) => {
const clientIpConfig = getClientIpConfig?.();
return normalizeRateLimitClientIp(resolveRequestClientIp(req, clientIpConfig?.trustedProxies, clientIpConfig?.allowRealIpFallback === true) ?? req.socket?.remoteAddress);
};
const pruneHookReplayCache = (now) => {
const cutoff = now - DEDUPE_TTL_MS;
for (const [key, entry] of hookReplayCache) if (entry.ts < cutoff) hookReplayCache.delete(key);
while (hookReplayCache.size > DEDUPE_MAX) {
const oldestKey = hookReplayCache.keys().next().value;
if (!oldestKey) break;
hookReplayCache.delete(oldestKey);
}
};
const buildHookReplayCacheKey = (params) => {
const idem = params.idempotencyKey?.trim();
if (!idem) return;
const tokenFingerprint = createHash("sha256").update(params.token ?? "", "utf8").digest("hex");
const idempotencyFingerprint = createHash("sha256").update(idem, "utf8").digest("hex");
return `${tokenFingerprint}:${createHash("sha256").update(JSON.stringify({
pathKey: params.pathKey,
dispatchScope: params.dispatchScope
}), "utf8").digest("hex")}:${idempotencyFingerprint}`;
};
const resolveCachedHookRunId = (key, now) => {
if (!key) return;
pruneHookReplayCache(now);
const cached = hookReplayCache.get(key);
if (!cached) return;
hookReplayCache.delete(key);
hookReplayCache.set(key, cached);
return cached.runId;
};
const rememberHookRunId = (key, runId, now) => {
if (!key) return;
hookReplayCache.delete(key);
hookReplayCache.set(key, {
ts: now,
runId
});
pruneHookReplayCache(now);
};
return async (req, res) => {
const hooksConfig = getHooksConfig();
if (!hooksConfig) return false;
const url = new URL(req.url ?? "/", "http://localhost");
const basePath = hooksConfig.basePath;
if (url.pathname !== basePath && !url.pathname.startsWith(`${basePath}/`)) return false;
if (url.searchParams.has("token")) {
res.statusCode = 400;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Hook token must be provided via Authorization: Bearer <token> or X-OpenClaw-Token header (query parameters are not allowed).");
return true;
}
if (req.method !== "POST") {
res.statusCode = 405;
res.setHeader("Allow", "POST");
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Method Not Allowed");
return true;
}
const token = extractHookToken(req);
const clientKey = resolveHookClientKey(req);
if (!safeEqualSecret(token, hooksConfig.token)) {
const throttle = hookAuthLimiter.check(clientKey, AUTH_RATE_LIMIT_SCOPE_HOOK_AUTH);
if (!throttle.allowed) {
const retryAfter = throttle.retryAfterMs > 0 ? Math.ceil(throttle.retryAfterMs / 1e3) : 1;
res.statusCode = 429;
res.setHeader("Retry-After", String(retryAfter));
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Too Many Requests");
logHooks.warn(`hook auth throttled for ${clientKey}; retry-after=${retryAfter}s`);
return true;
}
hookAuthLimiter.recordFailure(clientKey, AUTH_RATE_LIMIT_SCOPE_HOOK_AUTH);
res.statusCode = 401;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Unauthorized");
return true;
}
hookAuthLimiter.reset(clientKey, AUTH_RATE_LIMIT_SCOPE_HOOK_AUTH);
const subPath = url.pathname.slice(basePath.length).replace(/^\/+/, "");
if (!subPath) {
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Not Found");
return true;
}
const body = await readJsonBody(req, hooksConfig.maxBodyBytes);
if (!body.ok) {
sendJson(res, body.error === "payload too large" ? 413 : body.error === "request body timeout" ? 408 : 400, {
ok: false,
error: body.error
});
return true;
}
const payload = typeof body.value === "object" && body.value !== null ? body.value : {};
const headers = normalizeHookHeaders(req);
const idempotencyKey = resolveHookIdempotencyKey({
payload,
headers
});
const now = Date.now();
if (subPath === "wake") {
const normalized = normalizeWakePayload(payload);
if (!normalized.ok) {
sendJson(res, 400, {
ok: false,
error: normalized.error
});
return true;
}
dispatchWakeHook(normalized.value);
sendJson(res, 200, {
ok: true,
mode: normalized.value.mode
});
return true;
}
if (subPath === "agent") {
const normalized = normalizeAgentPayload(payload);
if (!normalized.ok) {
sendJson(res, 400, {
ok: false,
error: normalized.error
});
return true;
}
if (!isHookAgentAllowed(hooksConfig, normalized.value.agentId)) {
sendJson(res, 400, {
ok: false,
error: getHookAgentPolicyError()
});
return true;
}
const sessionKey = resolveHookSessionKey({
hooksConfig,
source: "request",
sessionKey: normalized.value.sessionKey
});
if (!sessionKey.ok) {
sendJson(res, 400, {
ok: false,
error: sessionKey.error
});
return true;
}
const targetAgentId = resolveHookTargetAgentId(hooksConfig, normalized.value.agentId);
const replayKey = buildHookReplayCacheKey({
pathKey: "agent",
token,
idempotencyKey,
dispatchScope: {
agentId: targetAgentId ?? null,
sessionKey: normalized.value.sessionKey ?? hooksConfig.sessionPolicy.defaultSessionKey ?? null,
message: normalized.value.message,
name: normalized.value.name,
wakeMode: normalized.value.wakeMode,
deliver: normalized.value.deliver,
channel: normalized.value.channel,
to: normalized.value.to ?? null,
model: normalized.value.model ?? null,
thinking: normalized.value.thinking ?? null,
timeoutSeconds: normalized.value.timeoutSeconds ?? null
}
});
const cachedRunId = resolveCachedHookRunId(replayKey, now);
if (cachedRunId) {
sendJson(res, 200, {
ok: true,
runId: cachedRunId
});
return true;
}
const normalizedDispatchSessionKey = normalizeHookDispatchSessionKey({
sessionKey: sessionKey.value,
targetAgentId
});
const runId = dispatchAgentHook({
...normalized.value,
idempotencyKey,
sessionKey: normalizedDispatchSessionKey,
agentId: targetAgentId,
externalContentSource: "webhook"
});
rememberHookRunId(replayKey, runId, now);
sendJson(res, 200, {
ok: true,
runId
});
return true;
}
if (hooksConfig.mappings.length > 0) try {
const mapped = await applyHookMappings(hooksConfig.mappings, {
payload,
headers,
url,
path: subPath
});
if (mapped) {
if (!mapped.ok) {
sendJson(res, 400, {
ok: false,
error: mapped.error
});
return true;
}
if (mapped.action === null) {
res.statusCode = 204;
res.end();
return true;
}
if (mapped.action.kind === "wake") {
dispatchWakeHook({
text: mapped.action.text,
mode: mapped.action.mode
});
sendJson(res, 200, {
ok: true,
mode: mapped.action.mode
});
return true;
}
const channel = resolveHookChannel(mapped.action.channel);
if (!channel) {
sendJson(res, 400, {
ok: false,
error: getHookChannelError()
});
return true;
}
if (!isHookAgentAllowed(hooksConfig, mapped.action.agentId)) {
sendJson(res, 400, {
ok: false,
error: getHookAgentPolicyError()
});
return true;
}
const sessionKey = resolveHookSessionKey({
hooksConfig,
source: "mapping",
sessionKey: mapped.action.sessionKey
});
if (!sessionKey.ok) {
sendJson(res, 400, {
ok: false,
error: sessionKey.error
});
return true;
}
const targetAgentId = resolveHookTargetAgentId(hooksConfig, mapped.action.agentId);
const normalizedDispatchSessionKey = normalizeHookDispatchSessionKey({
sessionKey: sessionKey.value,
targetAgentId
});
const replayKey = buildHookReplayCacheKey({
pathKey: subPath || "mapping",
token,
idempotencyKey,
dispatchScope: {
agentId: targetAgentId ?? null,
sessionKey: mapped.action.sessionKey ?? hooksConfig.sessionPolicy.defaultSessionKey ?? null,
message: mapped.action.message,
name: mapped.action.name ?? "Hook",
wakeMode: mapped.action.wakeMode,
deliver: resolveHookDeliver(mapped.action.deliver),
channel,
to: mapped.action.to ?? null,
model: mapped.action.model ?? null,
thinking: mapped.action.thinking ?? null,
timeoutSeconds: mapped.action.timeoutSeconds ?? null
}
});
const cachedRunId = resolveCachedHookRunId(replayKey, now);
if (cachedRunId) {
sendJson(res, 200, {
ok: true,
runId: cachedRunId
});
return true;
}
const runId = dispatchAgentHook({
message: mapped.action.message,
name: mapped.action.name ?? "Hook",
idempotencyKey,
agentId: targetAgentId,
wakeMode: mapped.action.wakeMode,
sessionKey: normalizedDispatchSessionKey,
deliver: resolveHookDeliver(mapped.action.deliver),
channel,
to: mapped.action.to,
model: mapped.action.model,
thinking: mapped.action.thinking,
timeoutSeconds: mapped.action.timeoutSeconds,
allowUnsafeExternalContent: mapped.action.allowUnsafeExternalContent,
externalContentSource: resolveMappedHookExternalContentSource({
subPath,
payload,
sessionKey: sessionKey.value
})
});
rememberHookRunId(replayKey, runId, now);
sendJson(res, 200, {
ok: true,
runId
});
return true;
}
} catch (err) {
logHooks.warn(`hook mapping failed: ${String(err)}`);
sendJson(res, 500, {
ok: false,
error: "hook mapping failed"
});
return true;
}
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Not Found");
return true;
};
}
function createGatewayHttpServer(opts) {
const { canvasHost, clients, controlUiEnabled, controlUiBasePath, controlUiRoot, openAiChatCompletionsEnabled, openAiChatCompletionsConfig, openResponsesEnabled, openResponsesConfig, strictTransportSecurityHeader, handleHooksRequest, handlePluginRequest, shouldEnforcePluginGatewayAuth, resolvedAuth, rateLimiter, getReadiness } = opts;
const httpServer = opts.tlsOptions ? createServer$1(opts.tlsOptions, (req, res) => {
handleRequest(req, res);
}) : createServer$2((req, res) => {
handleRequest(req, res);
});
async function handleRequest(req, res) {
setDefaultSecurityHeaders(res, { strictTransportSecurity: strictTransportSecurityHeader });
if (String(req.headers.upgrade ?? "").toLowerCase() === "websocket") return;
try {
const configSnapshot = loadConfig();
const trustedProxies = configSnapshot.gateway?.trustedProxies ?? [];
const allowRealIpFallback = configSnapshot.gateway?.allowRealIpFallback === true;
const scopedCanvas = normalizeCanvasScopedUrl(req.url ?? "/");
if (scopedCanvas.malformedScopedPath) {
sendGatewayAuthFailure(res, {
ok: false,
reason: "unauthorized"
});
return;
}
if (scopedCanvas.rewrittenUrl) req.url = scopedCanvas.rewrittenUrl;
const requestPath = new URL(req.url ?? "/", "http://localhost").pathname;
const mattermostSlashCallbackPaths = resolveMattermostSlashCallbackPaths(configSnapshot);
const pluginPathContext = handlePluginRequest ? resolvePluginRoutePathContext(requestPath) : null;
const requestStages = [
{
name: "hooks",
run: () => handleHooksRequest(req, res)
},
{
name: "tools-invoke",
run: () => handleToolsInvokeHttpRequest(req, res, {
auth: resolvedAuth,
trustedProxies,
allowRealIpFallback,
rateLimiter
})
},
{
name: "sessions-kill",
run: () => handleSessionKillHttpRequest(req, res, {
auth: resolvedAuth,
trustedProxies,
allowRealIpFallback,
rateLimiter
})
},
{
name: "sessions-history",
run: () => handleSessionHistoryHttpRequest(req, res, {
auth: resolvedAuth,
trustedProxies,
allowRealIpFallback,
rateLimiter
})
},
{
name: "slack",
run: () => handleSlackHttpRequest(req, res)
}
];
if (openResponsesEnabled) requestStages.push({
name: "openresponses",
run: () => handleOpenResponsesHttpRequest(req, res, {
auth: resolvedAuth,
config: openResponsesConfig,
trustedProxies,
allowRealIpFallback,
rateLimiter
})
});
if (openAiChatCompletionsEnabled) requestStages.push({
name: "openai",
run: () => handleOpenAiHttpRequest(req, res, {
auth: resolvedAuth,
config: openAiChatCompletionsConfig,
trustedProxies,
allowRealIpFallback,
rateLimiter
})
});
if (canvasHost) {
requestStages.push({
name: "canvas-auth",
run: async () => {
if (!isCanvasPath(requestPath)) return false;
const ok = await authorizeCanvasRequest({
req,
auth: resolvedAuth,
trustedProxies,
allowRealIpFallback,
clients,
canvasCapability: scopedCanvas.capability,
malformedScopedPath: scopedCanvas.malformedScopedPath,
rateLimiter
});
if (!ok.ok) {
sendGatewayAuthFailure(res, ok);
return true;
}
return false;
}
});
requestStages.push({
name: "a2ui",
run: () => handleA2uiHttpRequest(req, res)
});
requestStages.push({
name: "canvas-http",
run: () => canvasHost.handleHttpRequest(req, res)
});
}
requestStages.push(...buildPluginRequestStages({
req,
res,
requestPath,
mattermostSlashCallbackPaths,
pluginPathContext,
handlePluginRequest,
shouldEnforcePluginGatewayAuth,
resolvedAuth,
trustedProxies,
allowRealIpFallback,
rateLimiter
}));
if (controlUiEnabled) {
requestStages.push({
name: "control-ui-avatar",
run: () => handleControlUiAvatarRequest(req, res, {
basePath: controlUiBasePath,
resolveAvatar: (agentId) => resolveAgentAvatar(configSnapshot, agentId)
})
});
requestStages.push({
name: "control-ui-http",
run: () => handleControlUiHttpRequest(req, res, {
basePath: controlUiBasePath,
config: configSnapshot,
root: controlUiRoot
})
});
}
requestStages.push({
name: "gateway-probes",
run: () => handleGatewayProbeRequest(req, res, requestPath, resolvedAuth, trustedProxies, allowRealIpFallback, getReadiness)
});
if (await runGatewayHttpRequestStages(requestStages)) return;
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Not Found");
} catch {
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Internal Server Error");
}
}
return httpServer;
}
function attachGatewayUpgradeHandler(opts) {
const { httpServer, wss, canvasHost, clients, resolvedAuth, rateLimiter } = opts;
httpServer.on("upgrade", (req, socket, head) => {
(async () => {
const scopedCanvas = normalizeCanvasScopedUrl(req.url ?? "/");
if (scopedCanvas.malformedScopedPath) {
writeUpgradeAuthFailure(socket, {
ok: false,
reason: "unauthorized"
});
socket.destroy();
return;
}
if (scopedCanvas.rewrittenUrl) req.url = scopedCanvas.rewrittenUrl;
if (canvasHost) {
if (new URL(req.url ?? "/", "http://localhost").pathname === "/__openclaw__/ws") {
const configSnapshot = loadConfig();
const ok = await authorizeCanvasRequest({
req,
auth: resolvedAuth,
trustedProxies: configSnapshot.gateway?.trustedProxies ?? [],
allowRealIpFallback: configSnapshot.gateway?.allowRealIpFallback === true,
clients,
canvasCapability: scopedCanvas.capability,
malformedScopedPath: scopedCanvas.malformedScopedPath,
rateLimiter
});
if (!ok.ok) {
writeUpgradeAuthFailure(socket, ok);
socket.destroy();
return;
}
}
if (canvasHost.handleUpgrade(req, socket, head)) return;
}
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req);
});
})().catch(() => {
socket.destroy();
});
});
}
//#endregion
//#region src/gateway/server/hooks.ts
function resolveHookClientIpConfig(cfg) {
return {
trustedProxies: cfg.gateway?.trustedProxies,
allowRealIpFallback: cfg.gateway?.allowRealIpFallback === true
};
}
function createGatewayHooksRequestHandler(params) {
const { deps, getHooksConfig, getClientIpConfig, bindHost, port, logHooks } = params;
const dispatchWakeHook = (value) => {
const sessionKey = resolveMainSessionKeyFromConfig();
enqueueSystemEvent(value.text, { sessionKey });
if (value.mode === "now") requestHeartbeatNow({ reason: "hook:wake" });
};
const dispatchAgentHook = (value) => {
const sessionKey = normalizeHookDispatchSessionKey({
sessionKey: value.sessionKey,
targetAgentId: value.agentId
});
const mainSessionKey = resolveMainSessionKeyFromConfig();
const jobId = randomUUID();
const now = Date.now();
const job = {
id: jobId,
agentId: value.agentId,
name: value.name,
enabled: true,
createdAtMs: now,
updatedAtMs: now,
schedule: {
kind: "at",
at: new Date(now).toISOString()
},
sessionTarget: "isolated",
wakeMode: value.wakeMode,
payload: {
kind: "agentTurn",
message: value.message,
model: value.model,
thinking: value.thinking,
timeoutSeconds: value.timeoutSeconds,
deliver: value.deliver,
channel: value.channel,
to: value.to,
allowUnsafeExternalContent: value.allowUnsafeExternalContent,
externalContentSource: value.externalContentSource
},
state: { nextRunAtMs: now }
};
const runId = randomUUID();
(async () => {
try {
const result = await runCronIsolatedAgentTurn({
cfg: loadConfig(),
deps,
job,
message: value.message,
sessionKey,
lane: "cron",
deliveryContract: "shared"
});
const summary = result.summary?.trim() || result.error?.trim() || result.status;
const prefix = result.status === "ok" ? `Hook ${value.name}` : `Hook ${value.name} (${result.status})`;
if (!result.delivered) {
enqueueSystemEvent(`${prefix}: ${summary}`.trim(), { sessionKey: mainSessionKey });
if (value.wakeMode === "now") requestHeartbeatNow({ reason: `hook:${jobId}` });
}
} catch (err) {
logHooks.warn(`hook agent failed: ${String(err)}`);
enqueueSystemEvent(`Hook ${value.name} (error): ${String(err)}`, { sessionKey: mainSessionKey });
if (value.wakeMode === "now") requestHeartbeatNow({ reason: `hook:${jobId}:error` });
}
})();
return runId;
};
return createHooksRequestHandler({
getHooksConfig,
bindHost,
port,
logHooks,
getClientIpConfig,
dispatchAgentHook,
dispatchWakeHook
});
}
//#endregion
//#region src/gateway/server-reload-handlers.ts
function createGatewayReloadHandlers(params) {
const applyHotReload = async (plan, nextConfig) => {
setGatewaySigusr1RestartPolicy({ allowExternal: isRestartEnabled(nextConfig) });
const state = params.getState();
const nextState = { ...state };
if (plan.reloadHooks) try {
nextState.hooksConfig = resolveHooksConfig(nextConfig);
} catch (err) {
params.logHooks.warn(`hooks config reload failed: ${String(err)}`);
}
nextState.hookClientIpConfig = resolveHookClientIpConfig(nextConfig);
if (plan.restartHeartbeat) nextState.heartbeatRunner.updateConfig(nextConfig);
resetDirectoryCache();
if (plan.restartCron) {
state.cronState.cron.stop();
nextState.cronState = buildGatewayCronService({
cfg: nextConfig,
deps: params.deps,
broadcast: params.broadcast
});
nextState.cronState.cron.start().catch((err) => params.logCron.error(`failed to start: ${String(err)}`));
}
if (plan.restartBrowserControl) {
if (state.browserControl) await state.browserControl.stop().catch(() => {});
try {
nextState.browserControl = await startBrowserControlServerIfEnabled();
} catch (err) {
params.logBrowser.error(`server failed to start: ${String(err)}`);
}
}
if (plan.restartHealthMonitor) {
state.channelHealthMonitor?.stop();
const minutes = nextConfig.gateway?.channelHealthCheckMinutes;
const staleMinutes = nextConfig.gateway?.channelStaleEventThresholdMinutes;
nextState.channelHealthMonitor = minutes === 0 ? null : params.createHealthMonitor({
checkIntervalMs: (minutes ?? 5) * 6e4,
...staleMinutes != null && { staleEventThresholdMs: staleMinutes * 6e4 },
...nextConfig.gateway?.channelMaxRestartsPerHour != null && { maxRestartsPerHour: nextConfig.gateway.channelMaxRestartsPerHour }
});
}
if (plan.restartGmailWatcher) {
await stopGmailWatcher().catch(() => {});
await startGmailWatcherWithLogs({
cfg: nextConfig,
log: params.logHooks,
onSkipped: () => params.logHooks.info("skipping gmail watcher restart (OPENCLAW_SKIP_GMAIL_WATCHER=1)")
});
}
if (plan.restartChannels.size > 0) if (isTruthyEnvValue(process.env.OPENCLAW_SKIP_CHANNELS) || isTruthyEnvValue(process.env.OPENCLAW_SKIP_PROVIDERS)) params.logChannels.info("skipping channel reload (OPENCLAW_SKIP_CHANNELS=1 or OPENCLAW_SKIP_PROVIDERS=1)");
else {
const restartChannel = async (name) => {
params.logChannels.info(`restarting ${name} channel`);
await params.stopChannel(name);
await params.startChannel(name);
};
for (const channel of plan.restartChannels) await restartChannel(channel);
}
setCommandLaneConcurrency(CommandLane.Cron, nextConfig.cron?.maxConcurrentRuns ?? 1);
setCommandLaneConcurrency(CommandLane.Main, resolveAgentMaxConcurrent(nextConfig));
setCommandLaneConcurrency(CommandLane.Subagent, resolveSubagentMaxConcurrent(nextConfig));
if (plan.hotReasons.length > 0) params.logReload.info(`config hot reload applied (${plan.hotReasons.join(", ")})`);
else if (plan.noopPaths.length > 0) params.logReload.info(`config change applied (dynamic reads: ${plan.noopPaths.join(", ")})`);
params.setState(nextState);
};
let restartPending = false;
const requestGatewayRestart = (plan, nextConfig) => {
setGatewaySigusr1RestartPolicy({ allowExternal: isRestartEnabled(nextConfig) });
const reasons = plan.restartReasons.length ? plan.restartReasons.join(", ") : plan.changedPaths.join(", ");
if (process.listenerCount("SIGUSR1") === 0) {
params.logReload.warn("no SIGUSR1 listener found; restart skipped");
return;
}
const getActiveCounts = () => {
const queueSize = getTotalQueueSize();
const pendingReplies = getTotalPendingReplies();
const embeddedRuns = getActiveEmbeddedRunCount();
return {
queueSize,
pendingReplies,
embeddedRuns,
totalActive: queueSize + pendingReplies + embeddedRuns
};
};
const formatActiveDetails = (counts) => {
const details = [];
if (counts.queueSize > 0) details.push(`${counts.queueSize} operation(s)`);
if (counts.pendingReplies > 0) details.push(`${counts.pendingReplies} reply(ies)`);
if (counts.embeddedRuns > 0) details.push(`${counts.embeddedRuns} embedded run(s)`);
return details;
};
const active = getActiveCounts();
if (active.totalActive > 0) {
if (restartPending) {
params.logReload.info(`config change requires gateway restart (${reasons}) — already waiting for operations to complete`);
return;
}
restartPending = true;
const initialDetails = formatActiveDetails(active);
params.logReload.warn(`config change requires gateway restart (${reasons}) — deferring until ${initialDetails.join(", ")} complete`);
deferGatewayRestartUntilIdle({
getPendingCount: () => getActiveCounts().totalActive,
maxWaitMs: nextConfig.gateway?.reload?.deferralTimeoutMs,
hooks: {
onReady: () => {
restartPending = false;
params.logReload.info("all operations and replies completed; restarting gateway now");
},
onTimeout: (_pending, elapsedMs) => {
const remaining = formatActiveDetails(getActiveCounts());
restartPending = false;
params.logReload.warn(`restart timeout after ${elapsedMs}ms with ${remaining.join(", ")} still active; restarting anyway`);
},
onCheckError: (err) => {
restartPending = false;
params.logReload.warn(`restart deferral check failed (${String(err)}); restarting gateway now`);
}
}
});
} else {
params.logReload.warn(`config change requires gateway restart (${reasons})`);
if (!emitGatewayRestart()) params.logReload.info("gateway restart already scheduled; skipping duplicate signal");
}
};
return {
applyHotReload,
requestGatewayRestart
};
}
//#endregion
//#region src/gateway/server-runtime-config.ts
async function resolveGatewayRuntimeConfig(params) {
const bindMode = params.bind ?? params.cfg.gateway?.bind ?? "loopback";
const customBindHost = params.cfg.gateway?.customBindHost;
const bindHost = params.host ?? await resolveGatewayBindHost(bindMode, customBindHost);
if (bindMode === "loopback" && !isLoopbackHost(bindHost)) throw new Error(`gateway bind=loopback resolved to non-loopback host ${bindHost}; refusing fallback to a network bind`);
if (bindMode === "custom") {
const configuredCustomBindHost = customBindHost?.trim();
if (!configuredCustomBindHost) throw new Error("gateway.bind=custom requires gateway.customBindHost");
if (!isValidIPv4(configuredCustomBindHost)) throw new Error(`gateway.bind=custom requires a valid IPv4 customBindHost (got ${configuredCustomBindHost})`);
if (bindHost !== configuredCustomBindHost) throw new Error(`gateway bind=custom requested ${configuredCustomBindHost} but resolved ${bindHost}; refusing fallback`);
}
const controlUiEnabled = params.controlUiEnabled ?? params.cfg.gateway?.controlUi?.enabled ?? true;
const openAiChatCompletionsConfig = params.cfg.gateway?.http?.endpoints?.chatCompletions;
const openAiChatCompletionsEnabled = params.openAiChatCompletionsEnabled ?? openAiChatCompletionsConfig?.enabled ?? false;
const openResponsesConfig = params.cfg.gateway?.http?.endpoints?.responses;
const openResponsesEnabled = params.openResponsesEnabled ?? openResponsesConfig?.enabled ?? false;
const strictTransportSecurityConfig = params.cfg.gateway?.http?.securityHeaders?.strictTransportSecurity;
const strictTransportSecurityHeader = strictTransportSecurityConfig === false ? void 0 : typeof strictTransportSecurityConfig === "string" && strictTransportSecurityConfig.trim().length > 0 ? strictTransportSecurityConfig.trim() : void 0;
const controlUiBasePath = normalizeControlUiBasePath(params.cfg.gateway?.controlUi?.basePath);
const controlUiRootRaw = params.cfg.gateway?.controlUi?.root;
const controlUiRoot = typeof controlUiRootRaw === "string" && controlUiRootRaw.trim().length > 0 ? controlUiRootRaw.trim() : void 0;
const tailscaleConfig = mergeGatewayTailscaleConfig(params.cfg.gateway?.tailscale ?? {}, params.tailscale ?? {});
const tailscaleMode = tailscaleConfig.mode ?? "off";
const resolvedAuth = resolveGatewayAuth({
authConfig: params.cfg.gateway?.auth,
authOverride: params.auth,
env: process.env,
tailscaleMode
});
const authMode = resolvedAuth.mode;
const hasToken = typeof resolvedAuth.token === "string" && resolvedAuth.token.trim().length > 0;
const hasPassword = typeof resolvedAuth.password === "string" && resolvedAuth.password.trim().length > 0;
const hasSharedSecret = authMode === "token" && hasToken || authMode === "password" && hasPassword;
const hooksConfig = resolveHooksConfig(params.cfg);
const canvasHostEnabled = process.env.OPENCLAW_SKIP_CANVAS_HOST !== "1" && params.cfg.canvasHost?.enabled !== false;
const trustedProxies = params.cfg.gateway?.trustedProxies ?? [];
const controlUiAllowedOrigins = (params.cfg.gateway?.controlUi?.allowedOrigins ?? []).map((value) => value.trim()).filter(Boolean);
const dangerouslyAllowHostHeaderOriginFallback = params.cfg.gateway?.controlUi?.dangerouslyAllowHostHeaderOriginFallback === true;
assertGatewayAuthConfigured(resolvedAuth, params.cfg.gateway?.auth);
if (tailscaleMode === "funnel" && authMode !== "password") throw new Error("tailscale funnel requires gateway auth mode=password (set gateway.auth.password or OPENCLAW_GATEWAY_PASSWORD)");
if (tailscaleMode !== "off" && !isLoopbackHost(bindHost)) throw new Error("tailscale serve/funnel requires gateway bind=loopback (127.0.0.1)");
if (!isLoopbackHost(bindHost) && !hasSharedSecret && authMode !== "trusted-proxy") throw new Error(`refusing to bind gateway to ${bindHost}:${params.port} without auth (set gateway.auth.token/password, or set OPENCLAW_GATEWAY_TOKEN/OPENCLAW_GATEWAY_PASSWORD)`);
if (controlUiEnabled && !isLoopbackHost(bindHost) && controlUiAllowedOrigins.length === 0 && !dangerouslyAllowHostHeaderOriginFallback) throw new Error("non-loopback Control UI requires gateway.controlUi.allowedOrigins (set explicit origins), or set gateway.controlUi.dangerouslyAllowHostHeaderOriginFallback=true to use Host-header origin fallback mode");
if (authMode === "trusted-proxy") {
if (trustedProxies.length === 0) throw new Error("gateway auth mode=trusted-proxy requires gateway.trustedProxies to be configured with at least one proxy IP");
if (isLoopbackHost(bindHost)) {
if (!(isTrustedProxyAddress("127.0.0.1", trustedProxies) || isTrustedProxyAddress("::1", trustedProxies))) throw new Error("gateway auth mode=trusted-proxy with bind=loopback requires gateway.trustedProxies to include 127.0.0.1, ::1, or a loopback CIDR");
}
}
return {
bindHost,
controlUiEnabled,
openAiChatCompletionsEnabled,
openAiChatCompletionsConfig: openAiChatCompletionsConfig ? {
...openAiChatCompletionsConfig,
enabled: openAiChatCompletionsEnabled
} : void 0,
openResponsesEnabled,
openResponsesConfig: openResponsesConfig ? {
...openResponsesConfig,
enabled: openResponsesEnabled
} : void 0,
strictTransportSecurityHeader,
controlUiBasePath,
controlUiRoot,
resolvedAuth,
authMode,
tailscaleConfig,
tailscaleMode,
hooksConfig,
canvasHostEnabled
};
}
//#endregion
//#region src/canvas-host/server.ts
function defaultIndexHTML() {
return `<!doctype html>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>OpenClaw Canvas</title>
<style>
html, body { height: 100%; margin: 0; background: #000; color: #fff; font: 16px/1.4 -apple-system, BlinkMacSystemFont, system-ui, Segoe UI, Roboto, Helvetica, Arial, sans-serif; }
.wrap { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.card { width: min(720px, 100%); background: rgba(255,255,255,0.06); border: 1px solid rgba(255,255,255,0.10); border-radius: 16px; padding: 18px 18px 14px; }
.title { display: flex; align-items: baseline; gap: 10px; }
h1 { margin: 0; font-size: 22px; letter-spacing: 0.2px; }
.sub { opacity: 0.75; font-size: 13px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
button { appearance: none; border: 1px solid rgba(255,255,255,0.14); background: rgba(255,255,255,0.10); color: #fff; padding: 10px 12px; border-radius: 12px; font-weight: 600; cursor: pointer; }
button:active { transform: translateY(1px); }
.ok { color: #24e08a; }
.bad { color: #ff5c5c; }
.log { margin-top: 14px; opacity: 0.85; font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace; white-space: pre-wrap; background: rgba(0,0,0,0.35); border: 1px solid rgba(255,255,255,0.08); padding: 10px; border-radius: 12px; }
</style>
<div class="wrap">
<div class="card">
<div class="title">
<h1>OpenClaw Canvas</h1>
<div class="sub">Interactive test page (auto-reload enabled)</div>
</div>
<div class="row">
<button id="btn-hello">Hello</button>
<button id="btn-time">Time</button>
<button id="btn-photo">Photo</button>
<button id="btn-dalek">Dalek</button>
</div>
<div id="status" class="sub" style="margin-top: 10px;"></div>
<div id="log" class="log">Ready.</div>
</div>
</div>
<script>
(() => {
const logEl = document.getElementById("log");
const statusEl = document.getElementById("status");
const log = (msg) => { logEl.textContent = String(msg); };
const hasIOS = () =>
!!(
window.webkit &&
window.webkit.messageHandlers &&
window.webkit.messageHandlers.openclawCanvasA2UIAction
);
const hasAndroid = () =>
!!(
(window.openclawCanvasA2UIAction &&
typeof window.openclawCanvasA2UIAction.postMessage === "function")
);
const hasHelper = () => typeof window.openclawSendUserAction === "function";
statusEl.innerHTML =
"Bridge: " +
(hasHelper() ? "<span class='ok'>ready</span>" : "<span class='bad'>missing</span>") +
" · iOS=" + (hasIOS() ? "yes" : "no") +
" · Android=" + (hasAndroid() ? "yes" : "no");
const onStatus = (ev) => {
const d = ev && ev.detail || {};
log("Action status: id=" + (d.id || "?") + " ok=" + String(!!d.ok) + (d.error ? (" error=" + d.error) : ""));
};
window.addEventListener("openclaw:a2ui-action-status", onStatus);
function send(name, sourceComponentId) {
if (!hasHelper()) {
log("No action bridge found. Ensure you're viewing this on an iOS/Android OpenClaw node canvas.");
return;
}
const sendUserAction =
typeof window.openclawSendUserAction === "function"
? window.openclawSendUserAction
: undefined;
const ok = sendUserAction({
name,
surfaceId: "main",
sourceComponentId,
context: { t: Date.now() },
});
log(ok ? ("Sent action: " + name) : ("Failed to send action: " + name));
}
document.getElementById("btn-hello").onclick = () => send("hello", "demo.hello");
document.getElementById("btn-time").onclick = () => send("time", "demo.time");
document.getElementById("btn-photo").onclick = () => send("photo", "demo.photo");
document.getElementById("btn-dalek").onclick = () => send("dalek", "demo.dalek");
})();
<\/script>
`;
}
function isDisabledByEnv() {
if (isTruthyEnvValue(process.env.OPENCLAW_SKIP_CANVAS_HOST)) return true;
if (isTruthyEnvValue(process.env.OPENCLAW_SKIP_CANVAS_HOST)) return true;
if (process.env.VITEST) return true;
return false;
}
function normalizeBasePath(rawPath) {
const normalized = normalizeUrlPath((rawPath ?? "/__openclaw__/canvas").trim() || "/__openclaw__/canvas");
if (normalized === "/") return "/";
return normalized.replace(/\/+$/, "");
}
async function prepareCanvasRoot(rootDir) {
await ensureDir(rootDir);
const rootReal = await fs$1.realpath(rootDir);
try {
const indexPath = path.join(rootReal, "index.html");
await fs$1.stat(indexPath);
} catch {
try {
await fs$1.writeFile(path.join(rootReal, "index.html"), defaultIndexHTML(), "utf8");
} catch {}
}
return rootReal;
}
function resolveDefaultCanvasRoot() {
const candidates = [path.join(resolveStateDir(), "canvas")];
return candidates.find((dir) => {
try {
return fsSync.statSync(dir).isDirectory();
} catch {
return false;
}
}) ?? candidates[0];
}
function resolveDefaultWatchFactory() {
const importedWatch = chokidar?.watch;
if (typeof importedWatch === "function") return importedWatch.bind(chokidar);
const runtime = createRequire(import.meta.url)("chokidar");
if (runtime && typeof runtime.watch === "function") return runtime.watch.bind(runtime);
if (runtime?.default && typeof runtime.default.watch === "function") return runtime.default.watch.bind(runtime.default);
throw new Error("chokidar.watch unavailable");
}
async function createCanvasHostHandler(opts) {
const basePath = normalizeBasePath(opts.basePath);
if (isDisabledByEnv() && opts.allowInTests !== true) return {
rootDir: "",
basePath,
handleHttpRequest: async () => false,
handleUpgrade: () => false,
close: async () => {}
};
const rootDir = resolveUserPath(opts.rootDir ?? resolveDefaultCanvasRoot());
const rootReal = await prepareCanvasRoot(rootDir);
const liveReload = opts.liveReload !== false;
const testMode = opts.allowInTests === true;
const reloadDebounceMs = testMode ? 12 : 75;
const writeStabilityThresholdMs = testMode ? 12 : 75;
const writePollIntervalMs = testMode ? 5 : 10;
const WebSocketServerClass = opts.webSocketServerClass ?? WebSocketServer;
const wss = liveReload ? new WebSocketServerClass({ noServer: true }) : null;
const sockets = /* @__PURE__ */ new Set();
if (wss) wss.on("connection", (ws) => {
sockets.add(ws);
ws.on("close", () => sockets.delete(ws));
});
let debounce = null;
const broadcastReload = () => {
if (!liveReload) return;
for (const ws of sockets) try {
ws.send("reload");
} catch {}
};
const scheduleReload = () => {
if (debounce) clearTimeout$1(debounce);
debounce = setTimeout$1(() => {
debounce = null;
broadcastReload();
}, reloadDebounceMs);
debounce.unref?.();
};
let watcherClosed = false;
const watchFactory = opts.watchFactory ?? resolveDefaultWatchFactory();
const watcher = liveReload ? watchFactory(rootReal, {
ignoreInitial: true,
awaitWriteFinish: {
stabilityThreshold: writeStabilityThresholdMs,
pollInterval: writePollIntervalMs
},
usePolling: testMode,
ignored: [/(^|[\\/])\../, /(^|[\\/])node_modules([\\/]|$)/]
}) : null;
watcher?.on("all", () => scheduleReload());
watcher?.on("error", (err) => {
if (watcherClosed) return;
watcherClosed = true;
opts.runtime.error(`canvasHost watcher error: ${String(err)} (live reload disabled; consider canvasHost.liveReload=false or a smaller canvasHost.root)`);
watcher.close().catch(() => {});
});
const handleUpgrade = (req, socket, head) => {
if (!wss) return false;
if (new URL(req.url ?? "/", "http://localhost").pathname !== "/__openclaw__/ws") return false;
wss.handleUpgrade(req, socket, head, (ws) => {
wss.emit("connection", ws, req);
});
return true;
};
const handleHttpRequest = async (req, res) => {
const urlRaw = req.url;
if (!urlRaw) return false;
try {
const url = new URL(urlRaw, "http://localhost");
if (url.pathname === "/__openclaw__/ws") {
res.statusCode = liveReload ? 426 : 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end(liveReload ? "upgrade required" : "not found");
return true;
}
let urlPath = url.pathname;
if (basePath !== "/") {
if (urlPath !== basePath && !urlPath.startsWith(`${basePath}/`)) return false;
urlPath = urlPath === basePath ? "/" : urlPath.slice(basePath.length) || "/";
}
if (req.method !== "GET" && req.method !== "HEAD") {
res.statusCode = 405;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("Method Not Allowed");
return true;
}
const opened = await resolveFileWithinRoot(rootReal, urlPath);
if (!opened) {
if (urlPath === "/" || urlPath.endsWith("/")) {
res.statusCode = 404;
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(`<!doctype html><meta charset="utf-8" /><title>OpenClaw Canvas</title><pre>Missing file.\nCreate ${rootDir}/index.html</pre>`);
return true;
}
res.statusCode = 404;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("not found");
return true;
}
const { handle, realPath } = opened;
let data;
try {
data = await handle.readFile();
} finally {
await handle.close().catch(() => {});
}
const lower = realPath.toLowerCase();
const mime = lower.endsWith(".html") || lower.endsWith(".htm") ? "text/html" : await detectMime({ filePath: realPath }) ?? "application/octet-stream";
res.setHeader("Cache-Control", "no-store");
if (mime === "text/html") {
const html = data.toString("utf8");
res.setHeader("Content-Type", "text/html; charset=utf-8");
res.end(liveReload ? injectCanvasLiveReload(html) : html);
return true;
}
res.setHeader("Content-Type", mime);
res.end(data);
return true;
} catch (err) {
opts.runtime.error(`canvasHost request failed: ${String(err)}`);
res.statusCode = 500;
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.end("error");
return true;
}
};
return {
rootDir,
basePath,
handleHttpRequest,
handleUpgrade,
close: async () => {
if (debounce) clearTimeout$1(debounce);
watcherClosed = true;
await watcher?.close().catch(() => {});
for (const ws of sockets) try {
ws.terminate?.();
} catch {}
if (wss) await new Promise((resolve) => wss.close(() => resolve()));
}
};
}
//#endregion
//#region src/gateway/server-broadcast.ts
const EVENT_SCOPE_GUARDS = {
"exec.approval.requested": [APPROVALS_SCOPE],
"exec.approval.resolved": [APPROVALS_SCOPE],
"device.pair.requested": [PAIRING_SCOPE],
"device.pair.resolved": [PAIRING_SCOPE],
"node.pair.requested": [PAIRING_SCOPE],
"node.pair.resolved": [PAIRING_SCOPE],
"sessions.changed": [READ_SCOPE],
"session.message": [READ_SCOPE],
"session.tool": [READ_SCOPE]
};
function hasEventScope(client, event) {
const required = EVENT_SCOPE_GUARDS[event];
if (!required) return true;
if ((client.connect.role ?? "operator") !== "operator") return false;
const scopes = Array.isArray(client.connect.scopes) ? client.connect.scopes : [];
if (scopes.includes("operator.admin")) return true;
if (required.includes("operator.read")) return scopes.includes("operator.read") || scopes.includes("operator.write");
return required.some((scope) => scopes.includes(scope));
}
function createGatewayBroadcaster(params) {
let seq = 0;
const broadcastInternal = (event, payload, opts, targetConnIds) => {
if (params.clients.size === 0) return;
const eventSeq = Boolean(targetConnIds) ? void 0 : ++seq;
const frame = JSON.stringify({
type: "event",
event,
payload,
seq: eventSeq,
stateVersion: opts?.stateVersion
});
if (shouldLogWs()) {
const logMeta = {
event,
seq: eventSeq ?? "targeted",
clients: params.clients.size,
targets: targetConnIds ? targetConnIds.size : void 0,
dropIfSlow: opts?.dropIfSlow,
presenceVersion: opts?.stateVersion?.presence,
healthVersion: opts?.stateVersion?.health
};
if (event === "agent") Object.assign(logMeta, summarizeAgentEventForWsLog(payload));
logWs("out", "event", logMeta);
}
for (const c of params.clients) {
if (targetConnIds && !targetConnIds.has(c.connId)) continue;
if (!hasEventScope(c, event)) continue;
const slow = c.socket.bufferedAmount > MAX_BUFFERED_BYTES;
if (slow && opts?.dropIfSlow) continue;
if (slow) {
try {
c.socket.close(1008, "slow consumer");
} catch {}
continue;
}
try {
c.socket.send(frame);
} catch {}
}
};
const broadcast = (event, payload, opts) => broadcastInternal(event, payload, opts);
const broadcastToConnIds = (event, payload, connIds, opts) => {
if (connIds.size === 0) return;
broadcastInternal(event, payload, opts, connIds);
};
return {
broadcast,
broadcastToConnIds
};
}
//#endregion
//#region src/infra/gateway-lock.ts
const DEFAULT_TIMEOUT_MS = 5e3;
const DEFAULT_POLL_INTERVAL_MS = 100;
const DEFAULT_STALE_MS = 3e4;
const DEFAULT_PORT_PROBE_TIMEOUT_MS = 1e3;
var GatewayLockError = class extends Error {
constructor(message, cause) {
super(message);
this.cause = cause;
this.name = "GatewayLockError";
}
};
function readLinuxCmdline(pid) {
try {
return parseProcCmdline(fs.readFileSync(`/proc/${pid}/cmdline`, "utf8"));
} catch {
return null;
}
}
function readLinuxStartTime(pid) {
try {
const raw = fs.readFileSync(`/proc/${pid}/stat`, "utf8").trim();
const closeParen = raw.lastIndexOf(")");
if (closeParen < 0) return null;
const fields = raw.slice(closeParen + 1).trim().split(/\s+/);
const startTime = Number.parseInt(fields[19] ?? "", 10);
return Number.isFinite(startTime) ? startTime : null;
} catch {
return null;
}
}
async function checkPortFree(port, host = "127.0.0.1") {
return await new Promise((resolve) => {
const socket = net.createConnection({
port,
host
});
let settled = false;
const finish = (result) => {
if (settled) return;
settled = true;
clearTimeout(timer);
socket.removeAllListeners();
socket.destroy();
resolve(result);
};
const timer = setTimeout(() => {
finish(true);
}, DEFAULT_PORT_PROBE_TIMEOUT_MS);
socket.once("connect", () => {
finish(false);
});
socket.once("error", () => {
finish(true);
});
});
}
async function resolveGatewayOwnerStatus(pid, payload, platform, port) {
if (port != null) {
if (await checkPortFree(port)) return "dead";
}
if (!isPidAlive(pid)) return "dead";
if (platform !== "linux") return "alive";
const payloadStartTime = payload?.startTime;
if (Number.isFinite(payloadStartTime)) {
const currentStartTime = readLinuxStartTime(pid);
if (currentStartTime == null) return "unknown";
return currentStartTime === payloadStartTime ? "alive" : "dead";
}
const args = readLinuxCmdline(pid);
if (!args) return "unknown";
return isGatewayArgv(args) ? "alive" : "dead";
}
async function readLockPayload(lockPath) {
try {
const raw = await fs$1.readFile(lockPath, "utf8");
const parsed = JSON.parse(raw);
if (typeof parsed.pid !== "number") return null;
if (typeof parsed.createdAt !== "string") return null;
if (typeof parsed.configPath !== "string") return null;
const startTime = typeof parsed.startTime === "number" ? parsed.startTime : void 0;
return {
pid: parsed.pid,
createdAt: parsed.createdAt,
configPath: parsed.configPath,
startTime
};
} catch {
return null;
}
}
function resolveGatewayLockPath(env, lockDir = resolveGatewayLockDir()) {
const configPath = resolveConfigPath(env, resolveStateDir(env));
const hash = createHash("sha256").update(configPath).digest("hex").slice(0, 8);
return {
lockPath: path.join(lockDir, `gateway.${hash}.lock`),
configPath
};
}
async function acquireGatewayLock(opts = {}) {
const env = opts.env ?? process.env;
const allowInTests = opts.allowInTests === true;
if (env.OPENCLAW_ALLOW_MULTI_GATEWAY === "1" || !allowInTests && (env.VITEST || env.NODE_ENV === "test")) return null;
const timeoutMs = opts.timeoutMs ?? DEFAULT_TIMEOUT_MS;
const pollIntervalMs = opts.pollIntervalMs ?? DEFAULT_POLL_INTERVAL_MS;
const staleMs = opts.staleMs ?? DEFAULT_STALE_MS;
const platform = opts.platform ?? process.platform;
const port = opts.port;
const now = opts.now ?? Date.now;
const sleep = opts.sleep ?? (async (ms) => await new Promise((resolve) => setTimeout(resolve, ms)));
const { lockPath, configPath } = resolveGatewayLockPath(env, opts.lockDir);
await fs$1.mkdir(path.dirname(lockPath), { recursive: true });
const startedAt = now();
let lastPayload = null;
while (now() - startedAt < timeoutMs) try {
const handle = await fs$1.open(lockPath, "wx");
const startTime = platform === "linux" ? readLinuxStartTime(process.pid) : null;
const payload = {
pid: process.pid,
createdAt: new Date(now()).toISOString(),
configPath
};
if (typeof startTime === "number" && Number.isFinite(startTime)) payload.startTime = startTime;
await handle.writeFile(JSON.stringify(payload), "utf8");
return {
lockPath,
configPath,
release: async () => {
await handle.close().catch(() => void 0);
await fs$1.rm(lockPath, { force: true });
}
};
} catch (err) {
if (err.code !== "EEXIST") throw new GatewayLockError(`failed to acquire gateway lock at ${lockPath}`, err);
lastPayload = await readLockPayload(lockPath);
const ownerPid = lastPayload?.pid;
const ownerStatus = ownerPid ? await resolveGatewayOwnerStatus(ownerPid, lastPayload, platform, port) : "unknown";
if (ownerStatus === "dead" && ownerPid) {
await fs$1.rm(lockPath, { force: true });
continue;
}
if (ownerStatus !== "alive") {
let stale = false;
if (lastPayload?.createdAt) {
const createdAt = Date.parse(lastPayload.createdAt);
stale = Number.isFinite(createdAt) ? now() - createdAt > staleMs : false;
}
if (!stale) try {
const st = await fs$1.stat(lockPath);
stale = now() - st.mtimeMs > staleMs;
} catch {
stale = false;
}
if (stale) {
await fs$1.rm(lockPath, { force: true });
continue;
}
}
await sleep(pollIntervalMs);
}
throw new GatewayLockError(`gateway already running${lastPayload?.pid ? ` (pid ${lastPayload.pid})` : ""}; lock timeout after ${timeoutMs}ms`);
}
//#endregion
//#region src/gateway/server/http-listen.ts
const EADDRINUSE_MAX_RETRIES = 4;
const EADDRINUSE_RETRY_INTERVAL_MS = 500;
async function closeServerQuietly(httpServer) {
await new Promise((resolve) => {
try {
httpServer.close(() => resolve());
} catch {
resolve();
}
});
}
async function listenGatewayHttpServer(params) {
const { httpServer, bindHost, port } = params;
for (let attempt = 0;; attempt++) try {
await new Promise((resolve, reject) => {
const onError = (err) => {
httpServer.off("listening", onListening);
reject(err);
};
const onListening = () => {
httpServer.off("error", onError);
resolve();
};
httpServer.once("error", onError);
httpServer.once("listening", onListening);
httpServer.listen(port, bindHost);
});
return;
} catch (err) {
const code = err.code;
if (code === "EADDRINUSE" && attempt < EADDRINUSE_MAX_RETRIES) {
await closeServerQuietly(httpServer);
await sleep(EADDRINUSE_RETRY_INTERVAL_MS);
continue;
}
if (code === "EADDRINUSE") throw new GatewayLockError(`another gateway instance is already listening on ws://${bindHost}:${port}`, err);
throw new GatewayLockError(`failed to bind gateway socket on ws://${bindHost}:${port}: ${String(err)}`, err);
}
}
//#endregion
//#region src/gateway/server-runtime-state.ts
async function createGatewayRuntimeState(params) {
pinActivePluginHttpRouteRegistry(params.pluginRegistry);
try {
let canvasHost = null;
if (params.canvasHostEnabled) try {
const handler = await createCanvasHostHandler({
runtime: params.canvasRuntime,
rootDir: params.cfg.canvasHost?.root,
basePath: CANVAS_HOST_PATH,
allowInTests: params.allowCanvasHostInTests,
liveReload: params.cfg.canvasHost?.liveReload
});
if (handler.rootDir) {
canvasHost = handler;
params.logCanvas.info(`canvas host mounted at http://${params.bindHost}:${params.port}${CANVAS_HOST_PATH}/ (root ${handler.rootDir})`);
}
} catch (err) {
params.logCanvas.warn(`canvas host failed to start: ${String(err)}`);
}
const clients = /* @__PURE__ */ new Set();
const { broadcast, broadcastToConnIds } = createGatewayBroadcaster({ clients });
const handleHooksRequest = createGatewayHooksRequestHandler({
deps: params.deps,
getHooksConfig: params.hooksConfig,
getClientIpConfig: params.getHookClientIpConfig,
bindHost: params.bindHost,
port: params.port,
logHooks: params.logHooks
});
const handlePluginRequest = createGatewayPluginRequestHandler({
registry: params.pluginRegistry,
log: params.logPlugins
});
const shouldEnforcePluginGatewayAuth = (pathContext) => {
return shouldEnforceGatewayAuthForPluginPath(resolveActivePluginHttpRouteRegistry(params.pluginRegistry), pathContext);
};
const bindHosts = await resolveGatewayListenHosts(params.bindHost);
if (!isLoopbackHost(params.bindHost)) params.log.warn("⚠️ Gateway is binding to a non-loopback address. Ensure authentication is configured before exposing to public networks.");
if (params.cfg.gateway?.controlUi?.dangerouslyAllowHostHeaderOriginFallback === true) params.log.warn("⚠️ gateway.controlUi.dangerouslyAllowHostHeaderOriginFallback=true is enabled. Host-header origin fallback weakens origin checks and should only be used as break-glass.");
const httpServers = [];
const httpBindHosts = [];
for (const host of bindHosts) {
const httpServer = createGatewayHttpServer({
canvasHost,
clients,
controlUiEnabled: params.controlUiEnabled,
controlUiBasePath: params.controlUiBasePath,
controlUiRoot: params.controlUiRoot,
openAiChatCompletionsEnabled: params.openAiChatCompletionsEnabled,
openAiChatCompletionsConfig: params.openAiChatCompletionsConfig,
openResponsesEnabled: params.openResponsesEnabled,
openResponsesConfig: params.openResponsesConfig,
strictTransportSecurityHeader: params.strictTransportSecurityHeader,
handleHooksRequest,
handlePluginRequest,
shouldEnforcePluginGatewayAuth,
resolvedAuth: params.resolvedAuth,
rateLimiter: params.rateLimiter,
getReadiness: params.getReadiness,
tlsOptions: params.gatewayTls?.enabled ? params.gatewayTls.tlsOptions : void 0
});
try {
await listenGatewayHttpServer({
httpServer,
bindHost: host,
port: params.port
});
httpServers.push(httpServer);
httpBindHosts.push(host);
} catch (err) {
if (host === bindHosts[0]) throw err;
params.log.warn(`gateway: failed to bind loopback alias ${host}:${params.port} (${String(err)})`);
}
}
const httpServer = httpServers[0];
if (!httpServer) throw new Error("Gateway HTTP server failed to start");
const wss = new WebSocketServer({
noServer: true,
maxPayload: MAX_PREAUTH_PAYLOAD_BYTES
});
for (const server of httpServers) attachGatewayUpgradeHandler({
httpServer: server,
wss,
canvasHost,
clients,
resolvedAuth: params.resolvedAuth,
rateLimiter: params.rateLimiter
});
const agentRunSeq = /* @__PURE__ */ new Map();
const dedupe = /* @__PURE__ */ new Map();
const chatRunState = createChatRunState();
const chatRunRegistry = chatRunState.registry;
const chatRunBuffers = chatRunState.buffers;
const chatDeltaSentAt = chatRunState.deltaSentAt;
const chatDeltaLastBroadcastLen = chatRunState.deltaLastBroadcastLen;
const addChatRun = chatRunRegistry.add;
const removeChatRun = chatRunRegistry.remove;
const chatAbortControllers = /* @__PURE__ */ new Map();
const toolEventRecipients = createToolEventRecipientRegistry();
return {
canvasHost,
releasePluginRouteRegistry: () => releasePinnedPluginHttpRouteRegistry(params.pluginRegistry),
httpServer,
httpServers,
httpBindHosts,
wss,
clients,
broadcast,
broadcastToConnIds,
agentRunSeq,
dedupe,
chatRunState,
chatRunBuffers,
chatDeltaSentAt,
chatDeltaLastBroadcastLen,
addChatRun,
removeChatRun,
chatAbortControllers,
toolEventRecipients
};
} catch (err) {
releasePinnedPluginHttpRouteRegistry(params.pluginRegistry);
throw err;
}
}
//#endregion
//#region src/gateway/server-session-key.ts
const RUN_LOOKUP_CACHE_LIMIT = 256;
const RUN_LOOKUP_MISS_TTL_MS = 1e3;
const resolvedSessionKeyByRunId = /* @__PURE__ */ new Map();
function setResolvedSessionKeyCache(runId, sessionKey) {
if (!runId) return;
if (!resolvedSessionKeyByRunId.has(runId) && resolvedSessionKeyByRunId.size >= RUN_LOOKUP_CACHE_LIMIT) {
const oldest = resolvedSessionKeyByRunId.keys().next().value;
if (oldest) resolvedSessionKeyByRunId.delete(oldest);
}
resolvedSessionKeyByRunId.set(runId, {
sessionKey,
expiresAt: sessionKey === null ? Date.now() + RUN_LOOKUP_MISS_TTL_MS : null
});
}
function resolveSessionKeyForRun(runId) {
const cached = getAgentRunContext(runId)?.sessionKey;
if (cached) return cached;
const cachedLookup = resolvedSessionKeyByRunId.get(runId);
if (cachedLookup !== void 0) {
if (cachedLookup.sessionKey !== null) return cachedLookup.sessionKey;
if ((cachedLookup.expiresAt ?? 0) > Date.now()) return;
resolvedSessionKeyByRunId.delete(runId);
}
const { store } = loadCombinedSessionStoreForGateway(loadConfig());
const storeKey = resolvePreferredSessionKeyForSessionIdMatches(Object.entries(store).filter((entry) => entry[1]?.sessionId === runId), runId);
if (storeKey) {
const sessionKey = toAgentRequestSessionKey(storeKey) ?? storeKey;
registerAgentRunContext(runId, { sessionKey });
setResolvedSessionKeyCache(runId, sessionKey);
return sessionKey;
}
setResolvedSessionKeyCache(runId, null);
}
//#endregion
//#region src/gateway/server-startup-log.ts
function logGatewayStartup(params) {
const { provider: agentProvider, model: agentModel } = resolveConfiguredModelRef({
cfg: params.cfg,
defaultProvider: DEFAULT_PROVIDER,
defaultModel: DEFAULT_MODEL
});
const modelRef = `${agentProvider}/${agentModel}`;
params.log.info(`agent model: ${modelRef}`, { consoleMessage: `agent model: ${chalk.whiteBright(modelRef)}` });
const scheme = params.tlsEnabled ? "wss" : "ws";
const formatHost = (host) => host.includes(":") ? `[${host}]` : host;
const listenEndpoints = (params.bindHosts && params.bindHosts.length > 0 ? params.bindHosts : [params.bindHost]).map((host) => `${scheme}://${formatHost(host)}:${params.port}`);
params.log.info(`listening on ${listenEndpoints.join(", ")} (PID ${process.pid})`);
params.log.info(`log file: ${getResolvedLoggerSettings().file}`);
if (params.isNixMode) params.log.info("gateway: running in Nix mode (config managed externally)");
const enabledDangerousFlags = collectEnabledInsecureOrDangerousFlags(params.cfg);
if (enabledDangerousFlags.length > 0) {
const warning = `security warning: dangerous config flags enabled: ${enabledDangerousFlags.join(", ")}. Run \`openclaw security audit\`.`;
params.log.warn(warning);
}
}
//#endregion
//#region src/hooks/import-url.ts
/**
* Build an import URL for a hook handler module.
*
* Bundled hooks (shipped in dist/) are immutable between installs, so they
* can be imported without a cache-busting suffix — letting V8 reuse its
* module cache across gateway restarts.
*
* Workspace, managed, and plugin hooks may be edited by the user between
* restarts. For those we append `?t=<mtime>&s=<size>` so the module key
* reflects on-disk changes while staying stable for unchanged files.
*/
/**
* Sources whose handler files never change between `npm install` runs.
* Imports from these sources skip cache busting entirely.
*/
const IMMUTABLE_SOURCES = new Set(["openclaw-bundled"]);
function buildImportUrl(handlerPath, source) {
const base = pathToFileURL(handlerPath).href;
if (IMMUTABLE_SOURCES.has(source)) return base;
try {
const { mtimeMs, size } = fs.statSync(handlerPath);
return `${base}?t=${mtimeMs}&s=${size}`;
} catch {
return `${base}?t=${Date.now()}`;
}
}
//#endregion
//#region src/hooks/loader.ts
/**
* Dynamic loader for hook handlers
*
* Loads hook handlers from external modules based on configuration
* and from directory-based discovery (bundled, managed, workspace)
*/
const log$2 = createSubsystemLogger("hooks:loader");
function safeLogValue(value) {
return sanitizeForLog(value);
}
function maybeWarnTrustedHookSource(source) {
if (source === "openclaw-workspace") {
log$2.warn("Loading workspace hook code into the gateway process. Workspace hooks are trusted local code.");
return;
}
if (source === "openclaw-managed") log$2.warn("Loading managed hook code into the gateway process. Managed hooks are trusted local code.");
}
/**
* Load and register all hook handlers
*
* Loads hooks from both:
* 1. Directory-based discovery (bundled, managed, workspace)
* 2. Legacy config handlers (backwards compatibility)
*
* @param cfg - OpenClaw configuration
* @param workspaceDir - Workspace directory for hook discovery
* @returns Number of handlers successfully loaded
*
* @example
* ```ts
* const config = await loadConfig();
* const workspaceDir = resolveAgentWorkspaceDir(config, agentId);
* const count = await loadInternalHooks(config, workspaceDir);
* console.log(`Loaded ${count} hook handlers`);
* ```
*/
async function loadInternalHooks(cfg, workspaceDir, opts) {
if (!cfg.hooks?.internal?.enabled) return 0;
let loadedCount = 0;
try {
const eligible = loadWorkspaceHookEntries(workspaceDir, {
config: cfg,
managedHooksDir: opts?.managedHooksDir,
bundledHooksDir: opts?.bundledHooksDir
}).filter((entry) => shouldIncludeHook({
entry,
config: cfg
}));
for (const entry of eligible) try {
const hookBaseDir = resolveExistingRealpath(entry.hook.baseDir);
if (!hookBaseDir) {
log$2.error(`Hook '${safeLogValue(entry.hook.name)}' base directory is no longer readable: ${safeLogValue(entry.hook.baseDir)}`);
continue;
}
const opened = await openBoundaryFile({
absolutePath: entry.hook.handlerPath,
rootPath: hookBaseDir,
boundaryLabel: "hook directory"
});
if (!opened.ok) {
log$2.error(`Hook '${safeLogValue(entry.hook.name)}' handler path fails boundary checks: ${safeLogValue(entry.hook.handlerPath)}`);
continue;
}
const safeHandlerPath = opened.path;
fs.closeSync(opened.fd);
maybeWarnTrustedHookSource(entry.hook.source);
const mod = await import(buildImportUrl(safeHandlerPath, entry.hook.source));
const exportName = entry.metadata?.export ?? "default";
const handler = resolveFunctionModuleExport({
mod,
exportName
});
if (!handler) {
log$2.error(`Handler '${safeLogValue(exportName)}' from ${safeLogValue(entry.hook.name)} is not a function`);
continue;
}
const events = entry.metadata?.events ?? [];
if (events.length === 0) {
log$2.warn(`Hook '${safeLogValue(entry.hook.name)}' has no events defined in metadata`);
continue;
}
for (const event of events) registerInternalHook(event, handler);
log$2.info(`Registered hook: ${safeLogValue(entry.hook.name)} -> ${events.map((event) => safeLogValue(event)).join(", ")}${exportName !== "default" ? ` (export: ${safeLogValue(exportName)})` : ""}`);
loadedCount++;
} catch (err) {
log$2.error(`Failed to load hook ${safeLogValue(entry.hook.name)}: ${safeLogValue(err instanceof Error ? err.message : String(err))}`);
}
} catch (err) {
log$2.error(`Failed to load directory-based hooks: ${safeLogValue(err instanceof Error ? err.message : String(err))}`);
}
const handlers = cfg.hooks.internal.handlers ?? [];
for (const handlerConfig of handlers) try {
const rawModule = handlerConfig.module.trim();
if (!rawModule) {
log$2.error("Handler module path is empty");
continue;
}
if (path.isAbsolute(rawModule)) {
log$2.error(`Handler module path must be workspace-relative (got absolute path): ${safeLogValue(rawModule)}`);
continue;
}
const baseDir = path.resolve(workspaceDir);
const modulePath = path.resolve(baseDir, rawModule);
const baseDirReal = resolveExistingRealpath(baseDir);
if (!baseDirReal) {
log$2.error(`Workspace directory is no longer readable while loading hooks: ${safeLogValue(baseDir)}`);
continue;
}
const modulePathSafe = resolveExistingRealpath(modulePath);
if (!modulePathSafe) {
log$2.error(`Handler module path could not be resolved with realpath: ${safeLogValue(rawModule)}`);
continue;
}
const rel = path.relative(baseDirReal, modulePathSafe);
if (!rel || rel.startsWith("..") || path.isAbsolute(rel)) {
log$2.error(`Handler module path must stay within workspaceDir: ${safeLogValue(rawModule)}`);
continue;
}
const opened = await openBoundaryFile({
absolutePath: modulePathSafe,
rootPath: baseDirReal,
boundaryLabel: "workspace directory"
});
if (!opened.ok) {
log$2.error(`Handler module path fails boundary checks under workspaceDir: ${safeLogValue(rawModule)}`);
continue;
}
const safeModulePath = opened.path;
fs.closeSync(opened.fd);
log$2.warn(`Loading legacy internal hook module from workspace path ${safeLogValue(rawModule)}. Legacy hook modules are trusted local code.`);
const mod = await import(buildImportUrl(safeModulePath, "openclaw-workspace"));
const exportName = handlerConfig.export ?? "default";
const handler = resolveFunctionModuleExport({
mod,
exportName
});
if (!handler) {
log$2.error(`Handler '${safeLogValue(exportName)}' from ${safeLogValue(modulePath)} is not a function`);
continue;
}
registerInternalHook(handlerConfig.event, handler);
log$2.info(`Registered hook (legacy): ${safeLogValue(handlerConfig.event)} -> ${safeLogValue(modulePath)}${exportName !== "default" ? `#${safeLogValue(exportName)}` : ""}`);
loadedCount++;
} catch (err) {
log$2.error(`Failed to load hook handler from ${safeLogValue(handlerConfig.module)}: ${safeLogValue(err instanceof Error ? err.message : String(err))}`);
}
return loadedCount;
}
function resolveExistingRealpath(value) {
try {
return fs.realpathSync(value);
} catch {
return null;
}
}
//#endregion
//#region src/plugins/services.ts
const log$1 = createSubsystemLogger("plugins");
function createPluginLogger() {
return {
info: (msg) => log$1.info(msg),
warn: (msg) => log$1.warn(msg),
error: (msg) => log$1.error(msg),
debug: (msg) => log$1.debug(msg)
};
}
function createServiceContext(params) {
return {
config: params.config,
workspaceDir: params.workspaceDir,
stateDir: STATE_DIR,
logger: createPluginLogger()
};
}
async function startPluginServices(params) {
const running = [];
const serviceContext = createServiceContext({
config: params.config,
workspaceDir: params.workspaceDir
});
for (const entry of params.registry.services) {
const service = entry.service;
try {
await service.start(serviceContext);
running.push({
id: service.id,
stop: service.stop ? () => service.stop?.(serviceContext) : void 0
});
} catch (err) {
const error = err;
const stack = error?.stack?.trim();
log$1.error(`plugin service failed (${service.id}, plugin=${entry.pluginId}, root=${entry.rootDir ?? "unknown"}): ${error?.message ?? String(err)}${stack ? `\n${stack}` : ""}`);
}
}
return { stop: async () => {
for (const entry of running.toReversed()) {
if (!entry.stop) continue;
try {
await entry.stop();
} catch (err) {
log$1.warn(`plugin service stop failed (${entry.id}): ${String(err)}`);
}
}
} };
}
//#endregion
//#region src/gateway/server-restart-sentinel.ts
async function scheduleRestartSentinelWake(_params) {
const sentinel = await consumeRestartSentinel();
if (!sentinel) return;
const payload = sentinel.payload;
const sessionKey = payload.sessionKey?.trim();
const message = formatRestartSentinelMessage(payload);
const summary = summarizeRestartSentinel(payload);
if (!sessionKey) {
enqueueSystemEvent(message, { sessionKey: resolveMainSessionKeyFromConfig() });
return;
}
const { baseSessionKey, threadId: sessionThreadId } = parseSessionThreadInfo(sessionKey);
const { cfg, entry } = loadSessionEntry(sessionKey);
const parsedTarget = resolveAnnounceTargetFromKey(baseSessionKey ?? sessionKey);
const sentinelContext = payload.deliveryContext;
let sessionDeliveryContext = deliveryContextFromSession(entry);
if (!sessionDeliveryContext && baseSessionKey && baseSessionKey !== sessionKey) {
const { entry: baseEntry } = loadSessionEntry(baseSessionKey);
sessionDeliveryContext = deliveryContextFromSession(baseEntry);
}
const origin = mergeDeliveryContext(sentinelContext, mergeDeliveryContext(sessionDeliveryContext, parsedTarget ?? void 0));
const channelRaw = origin?.channel;
const channel = channelRaw ? normalizeChannelId(channelRaw) : null;
const to = origin?.to;
if (!channel || !to) {
enqueueSystemEvent(message, { sessionKey });
return;
}
const resolved = resolveOutboundTarget({
channel,
to,
cfg,
accountId: origin?.accountId,
mode: "implicit"
});
if (!resolved.ok) {
enqueueSystemEvent(message, { sessionKey });
return;
}
const threadId = payload.threadId ?? parsedTarget?.threadId ?? sessionThreadId ?? (origin?.threadId != null ? String(origin.threadId) : void 0);
const isSlack = channel === "slack";
const replyToId = isSlack && threadId != null && threadId !== "" ? String(threadId) : void 0;
const resolvedThreadId = isSlack ? void 0 : threadId;
const outboundSession = buildOutboundSessionContext({
cfg,
sessionKey
});
try {
await deliverOutboundPayloads({
cfg,
channel,
to: resolved.to,
accountId: origin?.accountId,
replyToId,
threadId: resolvedThreadId,
payloads: [{ text: message }],
session: outboundSession,
bestEffort: true
});
} catch (err) {
enqueueSystemEvent(`${summary}\n${String(err)}`, { sessionKey });
}
}
function shouldWakeFromRestartSentinel() {
return !process.env.VITEST && true;
}
//#endregion
//#region src/gateway/server-startup-memory.ts
async function startGatewayMemoryBackend(params) {
const agentIds = listAgentIds(params.cfg);
for (const agentId of agentIds) {
if (!resolveMemorySearchConfig(params.cfg, agentId)) continue;
const resolved = resolveMemoryBackendConfig({
cfg: params.cfg,
agentId
});
if (resolved.backend !== "qmd" || !resolved.qmd) continue;
const { manager, error } = await getMemorySearchManager({
cfg: params.cfg,
agentId
});
if (!manager) {
params.log.warn(`qmd memory startup initialization failed for agent "${agentId}": ${error ?? "unknown error"}`);
continue;
}
params.log.info?.(`qmd memory startup initialization armed for agent "${agentId}"`);
}
}
//#endregion
//#region src/gateway/server-startup.ts
const SESSION_LOCK_STALE_MS = 1800 * 1e3;
async function prewarmConfiguredPrimaryModel(params) {
if (!resolveAgentModelPrimaryValue(params.cfg.agents?.defaults?.model)?.trim()) return;
const { provider, model } = resolveConfiguredModelRef({
cfg: params.cfg,
defaultProvider: DEFAULT_PROVIDER,
defaultModel: DEFAULT_MODEL
});
const agentDir = resolveOpenClawAgentDir();
try {
await ensureOpenClawModelsJson(params.cfg, agentDir);
const resolved = await resolveModelAsync(provider, model, agentDir, params.cfg, { retryTransientProviderRuntimeMiss: true });
if (!resolved.model) throw new Error(resolved.error ?? `Unknown model: ${provider}/${model}`);
} catch (err) {
params.log.warn(`startup model warmup failed for ${provider}/${model}: ${String(err)}`);
}
}
async function startGatewaySidecars(params) {
try {
const sessionDirs = await resolveAgentSessionDirs(resolveStateDir(process.env));
for (const sessionsDir of sessionDirs) await cleanStaleLockFiles({
sessionsDir,
staleMs: SESSION_LOCK_STALE_MS,
removeStale: true,
log: { warn: (message) => params.log.warn(message) }
});
} catch (err) {
params.log.warn(`session lock cleanup failed on startup: ${String(err)}`);
}
let browserControl = null;
try {
browserControl = await startBrowserControlServerIfEnabled();
} catch (err) {
params.logBrowser.error(`server failed to start: ${String(err)}`);
}
await startGmailWatcherWithLogs({
cfg: params.cfg,
log: params.logHooks
});
if (params.cfg.hooks?.gmail?.model) {
const hooksModelRef = resolveHooksGmailModel({
cfg: params.cfg,
defaultProvider: DEFAULT_PROVIDER
});
if (hooksModelRef) {
const { provider: defaultProvider, model: defaultModel } = resolveConfiguredModelRef({
cfg: params.cfg,
defaultProvider: DEFAULT_PROVIDER,
defaultModel: DEFAULT_MODEL
});
const catalog = await loadModelCatalog({ config: params.cfg });
const status = getModelRefStatus({
cfg: params.cfg,
catalog,
ref: hooksModelRef,
defaultProvider,
defaultModel
});
if (!status.allowed) params.logHooks.warn(`hooks.gmail.model "${status.key}" not in agents.defaults.models allowlist (will use primary instead)`);
if (!status.inCatalog) params.logHooks.warn(`hooks.gmail.model "${status.key}" not in the model catalog (may fail at runtime)`);
}
}
try {
clearInternalHooks();
const loadedCount = await loadInternalHooks(params.cfg, params.defaultWorkspaceDir);
if (loadedCount > 0) params.logHooks.info(`loaded ${loadedCount} internal hook handler${loadedCount > 1 ? "s" : ""}`);
} catch (err) {
params.logHooks.error(`failed to load hooks: ${String(err)}`);
}
if (!(isTruthyEnvValue(process.env.OPENCLAW_SKIP_CHANNELS) || isTruthyEnvValue(process.env.OPENCLAW_SKIP_PROVIDERS))) try {
await prewarmConfiguredPrimaryModel({
cfg: params.cfg,
log: params.log
});
await params.startChannels();
} catch (err) {
params.logChannels.error(`channel startup failed: ${String(err)}`);
}
else params.logChannels.info("skipping channel start (OPENCLAW_SKIP_CHANNELS=1 or OPENCLAW_SKIP_PROVIDERS=1)");
if (params.cfg.hooks?.internal?.enabled) setTimeout(() => {
triggerInternalHook(createInternalHookEvent("gateway", "startup", "gateway:startup", {
cfg: params.cfg,
deps: params.deps,
workspaceDir: params.defaultWorkspaceDir
}));
}, 250);
let pluginServices = null;
try {
pluginServices = await startPluginServices({
registry: params.pluginRegistry,
config: params.cfg,
workspaceDir: params.defaultWorkspaceDir
});
} catch (err) {
params.log.warn(`plugin services failed to start: ${String(err)}`);
}
if (params.cfg.acp?.enabled) getAcpSessionManager().reconcilePendingSessionIdentities({ cfg: params.cfg }).then((result) => {
if (result.checked === 0) return;
params.log.warn(`acp startup identity reconcile (renderer=v1): checked=${result.checked} resolved=${result.resolved} failed=${result.failed}`);
}).catch((err) => {
params.log.warn(`acp startup identity reconcile failed: ${String(err)}`);
});
startGatewayMemoryBackend({
cfg: params.cfg,
log: params.log
}).catch((err) => {
params.log.warn(`qmd memory startup initialization failed: ${String(err)}`);
});
if (shouldWakeFromRestartSentinel()) setTimeout(() => {
scheduleRestartSentinelWake({ deps: params.deps });
}, 750);
return {
browserControl,
pluginServices
};
}
//#endregion
//#region src/gateway/server-tailscale.ts
async function startGatewayTailscaleExposure(params) {
if (params.tailscaleMode === "off") return null;
try {
if (params.tailscaleMode === "serve") await enableTailscaleServe(params.port);
else await enableTailscaleFunnel(params.port);
const host = await getTailnetHostname().catch(() => null);
if (host) {
const uiPath = params.controlUiBasePath ? `${params.controlUiBasePath}/` : "/";
params.logTailscale.info(`${params.tailscaleMode} enabled: https://${host}${uiPath} (WS via wss://${host})`);
} else params.logTailscale.info(`${params.tailscaleMode} enabled`);
} catch (err) {
params.logTailscale.warn(`${params.tailscaleMode} failed: ${err instanceof Error ? err.message : String(err)}`);
}
if (!params.resetOnExit) return null;
return async () => {
try {
if (params.tailscaleMode === "serve") await disableTailscaleServe();
else await disableTailscaleFunnel();
} catch (err) {
params.logTailscale.warn(`${params.tailscaleMode} cleanup failed: ${err instanceof Error ? err.message : String(err)}`);
}
};
}
//#endregion
//#region src/gateway/server-wizard-sessions.ts
function createWizardSessionTracker() {
const wizardSessions = /* @__PURE__ */ new Map();
const findRunningWizard = () => {
for (const [id, session] of wizardSessions) if (session.getStatus() === "running") return id;
return null;
};
const purgeWizardSession = (id) => {
const session = wizardSessions.get(id);
if (!session) return;
if (session.getStatus() === "running") return;
wizardSessions.delete(id);
};
return {
wizardSessions,
findRunningWizard,
purgeWizardSession
};
}
//#endregion
//#region src/infra/canvas-host-url.ts
const normalizeHost = (value, rejectLoopback) => {
if (!value) return "";
const trimmed = value.trim();
if (!trimmed) return "";
if (rejectLoopback && isLoopbackHost(trimmed)) return "";
return trimmed;
};
const parseHostHeader = (value) => {
if (!value) return { host: "" };
try {
const parsed = new URL(`http://${String(value).trim()}`);
const portRaw = parsed.port.trim();
const port = portRaw ? Number.parseInt(portRaw, 10) : void 0;
return {
host: parsed.hostname,
port: Number.isFinite(port) ? port : void 0
};
} catch {
return { host: "" };
}
};
const parseForwardedProto = (value) => {
if (Array.isArray(value)) return value[0];
return value;
};
function resolveCanvasHostUrl(params) {
const port = params.canvasPort;
if (!port) return;
const scheme = params.scheme ?? (parseForwardedProto(params.forwardedProto)?.trim() === "https" ? "https" : "http");
const override = normalizeHost(params.hostOverride, true);
const parsedRequestHost = parseHostHeader(params.requestHost);
const requestHost = normalizeHost(parsedRequestHost.host, !!override);
const localAddress = normalizeHost(params.localAddress, Boolean(override || requestHost));
const host = override || requestHost || localAddress;
if (!host) return;
let exposedPort = port;
if (!override && requestHost && port === 18789) {
if (parsedRequestHost.port && parsedRequestHost.port > 0) exposedPort = parsedRequestHost.port;
else if (scheme === "https") exposedPort = 443;
else if (scheme === "http") exposedPort = 80;
}
return `${scheme}://${host.includes(":") ? `[${host}]` : host}:${exposedPort}`;
}
//#endregion
//#region src/gateway/origin-check.ts
function parseOrigin(originRaw) {
const trimmed = (originRaw ?? "").trim();
if (!trimmed || trimmed === "null") return null;
try {
const url = new URL(trimmed);
return {
origin: url.origin.toLowerCase(),
host: url.host.toLowerCase(),
hostname: url.hostname.toLowerCase()
};
} catch {
return null;
}
}
function checkBrowserOrigin(params) {
const parsedOrigin = parseOrigin(params.origin);
if (!parsedOrigin) return {
ok: false,
reason: "origin missing or invalid"
};
const allowlist = new Set((params.allowedOrigins ?? []).map((value) => value.trim().toLowerCase()).filter(Boolean));
if (allowlist.has("*") || allowlist.has(parsedOrigin.origin)) return {
ok: true,
matchedBy: "allowlist"
};
const requestHost = normalizeHostHeader(params.requestHost);
if (params.allowHostHeaderOriginFallback === true && requestHost && parsedOrigin.host === requestHost) return {
ok: true,
matchedBy: "host-header-fallback"
};
if (params.isLocalClient && isLoopbackHost(parsedOrigin.hostname)) return {
ok: true,
matchedBy: "local-loopback"
};
return {
ok: false,
reason: "origin not allowed"
};
}
//#endregion
//#region src/gateway/server/ws-connection/auth-context.ts
function trimToUndefined(value) {
if (!value) return;
const trimmed = value.trim();
return trimmed.length > 0 ? trimmed : void 0;
}
function resolveSharedConnectAuth(connectAuth) {
const token = trimToUndefined(connectAuth?.token);
const password = trimToUndefined(connectAuth?.password);
if (!token && !password) return;
return {
token,
password
};
}
function resolveDeviceTokenCandidate(connectAuth) {
const explicitDeviceToken = trimToUndefined(connectAuth?.deviceToken);
if (explicitDeviceToken) return {
token: explicitDeviceToken,
source: "explicit-device-token"
};
const fallbackToken = trimToUndefined(connectAuth?.token);
if (!fallbackToken) return {};
return {
token: fallbackToken,
source: "shared-token-fallback"
};
}
function resolveBootstrapTokenCandidate(connectAuth) {
return trimToUndefined(connectAuth?.bootstrapToken);
}
async function resolveConnectAuthState(params) {
const sharedConnectAuth = resolveSharedConnectAuth(params.connectAuth);
const sharedAuthProvided = Boolean(sharedConnectAuth);
const bootstrapTokenCandidate = params.hasDeviceIdentity ? resolveBootstrapTokenCandidate(params.connectAuth) : void 0;
const { token: deviceTokenCandidate, source: deviceTokenCandidateSource } = params.hasDeviceIdentity ? resolveDeviceTokenCandidate(params.connectAuth) : {};
const hasDeviceTokenCandidate = Boolean(deviceTokenCandidate);
let authResult = await authorizeWsControlUiGatewayConnect({
auth: params.resolvedAuth,
connectAuth: sharedConnectAuth,
req: params.req,
trustedProxies: params.trustedProxies,
allowRealIpFallback: params.allowRealIpFallback,
rateLimiter: hasDeviceTokenCandidate ? void 0 : params.rateLimiter,
clientIp: params.clientIp,
rateLimitScope: AUTH_RATE_LIMIT_SCOPE_SHARED_SECRET
});
if (hasDeviceTokenCandidate && authResult.ok && params.rateLimiter && (authResult.method === "token" || authResult.method === "password")) {
const sharedRateCheck = params.rateLimiter.check(params.clientIp, AUTH_RATE_LIMIT_SCOPE_SHARED_SECRET);
if (!sharedRateCheck.allowed) authResult = {
ok: false,
reason: "rate_limited",
rateLimited: true,
retryAfterMs: sharedRateCheck.retryAfterMs
};
else params.rateLimiter.reset(params.clientIp, AUTH_RATE_LIMIT_SCOPE_SHARED_SECRET);
}
const sharedAuthResult = sharedConnectAuth && await authorizeHttpGatewayConnect({
auth: {
...params.resolvedAuth,
allowTailscale: false
},
connectAuth: sharedConnectAuth,
req: params.req,
trustedProxies: params.trustedProxies,
allowRealIpFallback: params.allowRealIpFallback,
rateLimitScope: "shared-secret"
});
const sharedAuthOk = sharedAuthResult?.ok === true && (sharedAuthResult.method === "token" || sharedAuthResult.method === "password") || authResult.ok && authResult.method === "trusted-proxy";
return {
authResult,
authOk: authResult.ok,
authMethod: authResult.method ?? (params.resolvedAuth.mode === "password" ? "password" : "token"),
sharedAuthOk,
sharedAuthProvided,
bootstrapTokenCandidate,
deviceTokenCandidate,
deviceTokenCandidateSource
};
}
async function resolveConnectAuthDecision(params) {
let authResult = params.state.authResult;
let authOk = params.state.authOk;
let authMethod = params.state.authMethod;
const bootstrapTokenCandidate = params.state.bootstrapTokenCandidate;
if (params.hasDeviceIdentity && params.deviceId && params.publicKey && !authOk && bootstrapTokenCandidate) {
const tokenCheck = await params.verifyBootstrapToken({
deviceId: params.deviceId,
publicKey: params.publicKey,
token: bootstrapTokenCandidate,
role: params.role,
scopes: params.scopes
});
if (tokenCheck.ok) {
authOk = true;
authMethod = "bootstrap-token";
} else authResult = {
ok: false,
reason: tokenCheck.reason ?? "bootstrap_token_invalid"
};
}
const deviceTokenCandidate = params.state.deviceTokenCandidate;
if (!params.hasDeviceIdentity || !params.deviceId || authOk || !deviceTokenCandidate) return {
authResult,
authOk,
authMethod
};
if (params.rateLimiter) {
const deviceRateCheck = params.rateLimiter.check(params.clientIp, AUTH_RATE_LIMIT_SCOPE_DEVICE_TOKEN);
if (!deviceRateCheck.allowed) authResult = {
ok: false,
reason: "rate_limited",
rateLimited: true,
retryAfterMs: deviceRateCheck.retryAfterMs
};
}
if (!authResult.rateLimited) if ((await params.verifyDeviceToken({
deviceId: params.deviceId,
token: deviceTokenCandidate,
role: params.role,
scopes: params.scopes
})).ok) {
authOk = true;
authMethod = "device-token";
params.rateLimiter?.reset(params.clientIp, AUTH_RATE_LIMIT_SCOPE_DEVICE_TOKEN);
} else {
authResult = {
ok: false,
reason: params.state.deviceTokenCandidateSource === "explicit-device-token" ? "device_token_mismatch" : authResult.reason ?? "device_token_mismatch"
};
params.rateLimiter?.recordFailure(params.clientIp, AUTH_RATE_LIMIT_SCOPE_DEVICE_TOKEN);
}
return {
authResult,
authOk,
authMethod
};
}
//#endregion
//#region src/gateway/server/ws-connection/auth-messages.ts
function formatGatewayAuthFailureMessage(params) {
const { authMode, authProvided, reason, client } = params;
const isCli = isGatewayCliClient(client);
const isControlUi = client?.id === GATEWAY_CLIENT_IDS.CONTROL_UI;
const isWebchat = isWebchatClient(client);
const tokenHint = isCli ? "set gateway.remote.token to match gateway.auth.token" : isControlUi || isWebchat ? "open the dashboard URL and paste the token in Control UI settings" : "provide gateway auth token";
const passwordHint = isCli ? "set gateway.remote.password to match gateway.auth.password" : isControlUi || isWebchat ? "enter the password in Control UI settings" : "provide gateway auth password";
switch (reason) {
case "token_missing": return `unauthorized: gateway token missing (${tokenHint})`;
case "token_mismatch": return `unauthorized: gateway token mismatch (${tokenHint})`;
case "token_missing_config": return "unauthorized: gateway token not configured on gateway (set gateway.auth.token)";
case "password_missing": return `unauthorized: gateway password missing (${passwordHint})`;
case "password_mismatch": return `unauthorized: gateway password mismatch (${passwordHint})`;
case "password_missing_config": return "unauthorized: gateway password not configured on gateway (set gateway.auth.password)";
case "bootstrap_token_invalid": return "unauthorized: bootstrap token invalid or expired (scan a fresh setup code)";
case "tailscale_user_missing": return "unauthorized: tailscale identity missing (use Tailscale Serve auth or gateway token/password)";
case "tailscale_proxy_missing": return "unauthorized: tailscale proxy headers missing (use Tailscale Serve or gateway token/password)";
case "tailscale_whois_failed": return "unauthorized: tailscale identity check failed (use Tailscale Serve auth or gateway token/password)";
case "tailscale_user_mismatch": return "unauthorized: tailscale identity mismatch (use Tailscale Serve auth or gateway token/password)";
case "rate_limited": return "unauthorized: too many failed authentication attempts (retry later)";
case "device_token_mismatch": return "unauthorized: device token mismatch (rotate/reissue device token)";
default: break;
}
if (authMode === "token" && authProvided === "none") return `unauthorized: gateway token missing (${tokenHint})`;
if (authMode === "token" && authProvided === "device-token") return "unauthorized: device token rejected (pair/repair this device, or provide gateway token)";
if (authProvided === "bootstrap-token") return "unauthorized: bootstrap token invalid or expired (scan a fresh setup code)";
if (authMode === "password" && authProvided === "none") return `unauthorized: gateway password missing (${passwordHint})`;
return "unauthorized";
}
//#endregion
//#region src/gateway/server/ws-connection/connect-policy.ts
function resolveControlUiAuthPolicy(params) {
const allowInsecureAuthConfigured = params.isControlUi && params.controlUiConfig?.allowInsecureAuth === true;
const dangerouslyDisableDeviceAuth = params.isControlUi && params.controlUiConfig?.dangerouslyDisableDeviceAuth === true;
return {
isControlUi: params.isControlUi,
allowInsecureAuthConfigured,
dangerouslyDisableDeviceAuth,
allowBypass: dangerouslyDisableDeviceAuth,
device: dangerouslyDisableDeviceAuth ? null : params.deviceRaw
};
}
function shouldSkipControlUiPairing(policy, role, trustedProxyAuthOk = false, authMode) {
if (trustedProxyAuthOk) return true;
if (policy.isControlUi && role === "operator" && authMode === "none") return true;
return role === "operator" && policy.allowBypass;
}
function isTrustedProxyControlUiOperatorAuth(params) {
return params.isControlUi && params.role === "operator" && params.authMode === "trusted-proxy" && params.authOk && params.authMethod === "trusted-proxy";
}
function evaluateMissingDeviceIdentity(params) {
if (params.hasDeviceIdentity) return { kind: "allow" };
if (params.isControlUi && params.trustedProxyAuthOk) return { kind: "allow" };
if (params.isControlUi && params.controlUiAuthPolicy.allowBypass && params.role === "operator") return { kind: "allow" };
if (params.isControlUi && !params.controlUiAuthPolicy.allowBypass) {
if (!params.controlUiAuthPolicy.allowInsecureAuthConfigured || !params.isLocalClient) return { kind: "reject-control-ui-insecure-auth" };
}
if (roleCanSkipDeviceIdentity(params.role, params.sharedAuthOk)) return { kind: "allow" };
if (!params.authOk && params.hasSharedAuth) return { kind: "reject-unauthorized" };
return { kind: "reject-device-required" };
}
//#endregion
//#region src/gateway/server/ws-connection/handshake-auth-helpers.ts
const BROWSER_ORIGIN_LOOPBACK_RATE_LIMIT_IP = "198.18.0.1";
function resolveHandshakeBrowserSecurityContext(params) {
const hasBrowserOriginHeader = Boolean(params.requestOrigin && params.requestOrigin.trim() !== "");
return {
hasBrowserOriginHeader,
enforceOriginCheckForAnyClient: hasBrowserOriginHeader,
rateLimitClientIp: hasBrowserOriginHeader && isLoopbackAddress(params.clientIp) ? BROWSER_ORIGIN_LOOPBACK_RATE_LIMIT_IP : params.clientIp,
authRateLimiter: hasBrowserOriginHeader && params.browserRateLimiter ? params.browserRateLimiter : params.rateLimiter
};
}
function shouldAllowSilentLocalPairing(params) {
return params.isLocalClient && (!params.hasBrowserOriginHeader || params.isControlUi || params.isWebchat) && (params.reason === "not-paired" || params.reason === "scope-upgrade");
}
function shouldSkipBackendSelfPairing(params) {
if (!(params.connectParams.client.id === GATEWAY_CLIENT_IDS.GATEWAY_CLIENT && params.connectParams.client.mode === GATEWAY_CLIENT_MODES.BACKEND)) return false;
const usesSharedSecretAuth = params.authMethod === "token" || params.authMethod === "password";
const usesDeviceTokenAuth = params.authMethod === "device-token";
return params.isLocalClient && !params.hasBrowserOriginHeader && (params.sharedAuthOk && usesSharedSecretAuth || usesDeviceTokenAuth);
}
function resolveSignatureToken(connectParams) {
return connectParams.auth?.token ?? connectParams.auth?.deviceToken ?? connectParams.auth?.bootstrapToken ?? null;
}
function buildUnauthorizedHandshakeContext(params) {
return {
authProvided: params.authProvided,
canRetryWithDeviceToken: params.canRetryWithDeviceToken,
recommendedNextStep: params.recommendedNextStep
};
}
function resolveDeviceSignaturePayloadVersion(params) {
const signatureToken = resolveSignatureToken(params.connectParams);
const basePayload = {
deviceId: params.device.id,
clientId: params.connectParams.client.id,
clientMode: params.connectParams.client.mode,
role: params.role,
scopes: params.scopes,
signedAtMs: params.signedAtMs,
token: signatureToken,
nonce: params.nonce
};
const payloadV3 = buildDeviceAuthPayloadV3({
...basePayload,
platform: params.connectParams.client.platform,
deviceFamily: params.connectParams.client.deviceFamily
});
if (verifyDeviceSignature(params.device.publicKey, payloadV3, params.device.signature)) return "v3";
const payloadV2 = buildDeviceAuthPayload(basePayload);
if (verifyDeviceSignature(params.device.publicKey, payloadV2, params.device.signature)) return "v2";
return null;
}
function resolveAuthProvidedKind(connectAuth) {
return connectAuth?.password ? "password" : connectAuth?.token ? "token" : connectAuth?.bootstrapToken ? "bootstrap-token" : connectAuth?.deviceToken ? "device-token" : "none";
}
function resolveUnauthorizedHandshakeContext(params) {
const authProvided = resolveAuthProvidedKind(params.connectAuth);
const canRetryWithDeviceToken = params.failedAuth.reason === "token_mismatch" && params.hasDeviceIdentity && authProvided === "token" && !params.connectAuth?.deviceToken;
if (canRetryWithDeviceToken) return buildUnauthorizedHandshakeContext({
authProvided,
canRetryWithDeviceToken,
recommendedNextStep: "retry_with_device_token"
});
switch (params.failedAuth.reason) {
case "token_missing":
case "token_missing_config":
case "password_missing":
case "password_missing_config": return buildUnauthorizedHandshakeContext({
authProvided,
canRetryWithDeviceToken,
recommendedNextStep: "update_auth_configuration"
});
case "token_mismatch":
case "password_mismatch":
case "device_token_mismatch": return buildUnauthorizedHandshakeContext({
authProvided,
canRetryWithDeviceToken,
recommendedNextStep: "update_auth_credentials"
});
case "rate_limited": return buildUnauthorizedHandshakeContext({
authProvided,
canRetryWithDeviceToken,
recommendedNextStep: "wait_then_retry"
});
default: return buildUnauthorizedHandshakeContext({
authProvided,
canRetryWithDeviceToken,
recommendedNextStep: "review_auth_configuration"
});
}
}
//#endregion
//#region src/gateway/server/ws-connection/unauthorized-flood-guard.ts
const DEFAULT_CLOSE_AFTER = 10;
const DEFAULT_LOG_EVERY = 100;
var UnauthorizedFloodGuard = class {
constructor(options) {
this.count = 0;
this.suppressedSinceLastLog = 0;
this.closeAfter = Math.max(1, Math.floor(options?.closeAfter ?? DEFAULT_CLOSE_AFTER));
this.logEvery = Math.max(1, Math.floor(options?.logEvery ?? DEFAULT_LOG_EVERY));
}
registerUnauthorized() {
this.count += 1;
const shouldClose = this.count > this.closeAfter;
if (!(this.count === 1 || this.count % this.logEvery === 0 || shouldClose)) {
this.suppressedSinceLastLog += 1;
return {
shouldClose,
shouldLog: false,
count: this.count,
suppressedSinceLastLog: 0
};
}
const suppressedSinceLastLog = this.suppressedSinceLastLog;
this.suppressedSinceLastLog = 0;
return {
shouldClose,
shouldLog: true,
count: this.count,
suppressedSinceLastLog
};
}
reset() {
this.count = 0;
this.suppressedSinceLastLog = 0;
}
};
function isUnauthorizedRoleError(error) {
if (!error) return false;
return error.code === ErrorCodes.INVALID_REQUEST && typeof error.message === "string" && error.message.startsWith("unauthorized role:");
}
//#endregion
//#region src/gateway/server/ws-connection/message-handler.ts
const DEVICE_SIGNATURE_SKEW_MS = 120 * 1e3;
function resolvePinnedClientMetadata(params) {
const claimedPlatform = normalizeDeviceMetadataForAuth(params.claimedPlatform);
const claimedDeviceFamily = normalizeDeviceMetadataForAuth(params.claimedDeviceFamily);
const pairedPlatform = normalizeDeviceMetadataForAuth(params.pairedPlatform);
const pairedDeviceFamily = normalizeDeviceMetadataForAuth(params.pairedDeviceFamily);
const hasPinnedPlatform = pairedPlatform !== "";
const hasPinnedDeviceFamily = pairedDeviceFamily !== "";
return {
platformMismatch: hasPinnedPlatform && claimedPlatform !== pairedPlatform,
deviceFamilyMismatch: hasPinnedDeviceFamily && claimedDeviceFamily !== pairedDeviceFamily,
pinnedPlatform: hasPinnedPlatform ? params.pairedPlatform : void 0,
pinnedDeviceFamily: hasPinnedDeviceFamily ? params.pairedDeviceFamily : void 0
};
}
function attachGatewayWsMessageHandler(params) {
const { socket, upgradeReq, connId, remoteAddr, forwardedFor, realIp, requestHost, requestOrigin, requestUserAgent, canvasHostUrl, connectNonce, resolvedAuth, rateLimiter, browserRateLimiter, gatewayMethods, events, extraHandlers, buildRequestContext, send, close, isClosed, clearHandshakeTimer, getClient, setClient, setHandshakeState, setCloseCause, setLastFrameMeta, originCheckMetrics, logGateway, logHealth, logWsControl } = params;
const configSnapshot = loadConfig();
const trustedProxies = configSnapshot.gateway?.trustedProxies ?? [];
const allowRealIpFallback = configSnapshot.gateway?.allowRealIpFallback === true;
const clientIp = resolveClientIp({
remoteAddr,
forwardedFor,
realIp,
trustedProxies,
allowRealIpFallback
});
const hasProxyHeaders = Boolean(forwardedFor || realIp);
const remoteIsTrustedProxy = isTrustedProxyAddress(remoteAddr, trustedProxies);
const hasUntrustedProxyHeaders = hasProxyHeaders && !remoteIsTrustedProxy;
const hostIsLocalish = isLocalishHost(requestHost);
const isLocalClient = isLocalDirectRequest(upgradeReq, trustedProxies, allowRealIpFallback);
const reportedClientIp = isLocalClient || hasUntrustedProxyHeaders ? void 0 : clientIp && !isLoopbackAddress(clientIp) ? clientIp : void 0;
if (hasUntrustedProxyHeaders) logWsControl.warn("Proxy headers detected from untrusted address. Connection will not be treated as local. Configure gateway.trustedProxies to restore local client detection behind your proxy.");
if (!hostIsLocalish && isLoopbackAddress(remoteAddr) && !hasProxyHeaders) logWsControl.warn("Loopback connection with non-local Host header. Treating it as remote. If you're behind a reverse proxy, set gateway.trustedProxies and forward X-Forwarded-For/X-Real-IP.");
const isWebchatConnect = (p) => isWebchatClient(p?.client);
const unauthorizedFloodGuard = new UnauthorizedFloodGuard();
const { hasBrowserOriginHeader, enforceOriginCheckForAnyClient, rateLimitClientIp: browserRateLimitClientIp, authRateLimiter } = resolveHandshakeBrowserSecurityContext({
requestOrigin,
clientIp,
rateLimiter,
browserRateLimiter
});
socket.on("message", async (data) => {
if (isClosed()) return;
const preauthPayloadBytes = !getClient() ? getRawDataByteLength(data) : void 0;
if (preauthPayloadBytes !== void 0 && preauthPayloadBytes > 65536) {
setHandshakeState("failed");
setCloseCause("preauth-payload-too-large", {
payloadBytes: preauthPayloadBytes,
limitBytes: MAX_PREAUTH_PAYLOAD_BYTES
});
close(1009, "preauth payload too large");
return;
}
const text = rawDataToString(data);
try {
const parsed = JSON.parse(text);
const frameType = parsed && typeof parsed === "object" && "type" in parsed ? typeof parsed.type === "string" ? String(parsed.type) : void 0 : void 0;
const frameMethod = parsed && typeof parsed === "object" && "method" in parsed ? typeof parsed.method === "string" ? String(parsed.method) : void 0 : void 0;
const frameId = parsed && typeof parsed === "object" && "id" in parsed ? typeof parsed.id === "string" ? String(parsed.id) : void 0 : void 0;
if (frameType || frameMethod || frameId) setLastFrameMeta({
type: frameType,
method: frameMethod,
id: frameId
});
const client = getClient();
if (!client) {
const isRequestFrame = validateRequestFrame(parsed);
if (!isRequestFrame || parsed.method !== "connect" || !validateConnectParams(parsed.params)) {
const handshakeError = isRequestFrame ? parsed.method === "connect" ? `invalid connect params: ${formatValidationErrors(validateConnectParams.errors)}` : "invalid handshake: first request must be connect" : "invalid request frame";
setHandshakeState("failed");
setCloseCause("invalid-handshake", {
frameType,
frameMethod,
frameId,
handshakeError
});
if (isRequestFrame) send({
type: "res",
id: parsed.id,
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, handshakeError)
});
else logWsControl.warn(`invalid handshake conn=${connId} remote=${remoteAddr ?? "?"} fwd=${forwardedFor ?? "n/a"} origin=${requestOrigin ?? "n/a"} host=${requestHost ?? "n/a"} ua=${requestUserAgent ?? "n/a"}`);
const closeReason = truncateCloseReason(handshakeError || "invalid handshake");
if (isRequestFrame) queueMicrotask(() => close(1008, closeReason));
else close(1008, closeReason);
return;
}
const frame = parsed;
const connectParams = frame.params;
const clientLabel = connectParams.client.displayName ?? connectParams.client.id;
const clientMeta = {
client: connectParams.client.id,
clientDisplayName: connectParams.client.displayName,
mode: connectParams.client.mode,
version: connectParams.client.version
};
const markHandshakeFailure = (cause, meta) => {
setHandshakeState("failed");
setCloseCause(cause, {
...meta,
...clientMeta
});
};
const sendHandshakeErrorResponse = (code, message, options) => {
send({
type: "res",
id: frame.id,
ok: false,
error: errorShape(code, message, options)
});
};
const { minProtocol, maxProtocol } = connectParams;
if (maxProtocol < 3 || minProtocol > 3) {
markHandshakeFailure("protocol-mismatch", {
minProtocol,
maxProtocol,
expectedProtocol: 3
});
logWsControl.warn(`protocol mismatch conn=${connId} remote=${remoteAddr ?? "?"} client=${clientLabel} ${connectParams.client.mode} v${connectParams.client.version}`);
sendHandshakeErrorResponse(ErrorCodes.INVALID_REQUEST, "protocol mismatch", { details: { expectedProtocol: 3 } });
close(1002, "protocol mismatch");
return;
}
const roleRaw = connectParams.role ?? "operator";
const role = parseGatewayRole(roleRaw);
if (!role) {
markHandshakeFailure("invalid-role", { role: roleRaw });
sendHandshakeErrorResponse(ErrorCodes.INVALID_REQUEST, "invalid role");
close(1008, "invalid role");
return;
}
let scopes = Array.isArray(connectParams.scopes) ? connectParams.scopes : [];
connectParams.role = role;
connectParams.scopes = scopes;
const isControlUi = connectParams.client.id === GATEWAY_CLIENT_IDS.CONTROL_UI;
const isWebchat = isWebchatConnect(connectParams);
if (enforceOriginCheckForAnyClient || isControlUi || isWebchat) {
const hostHeaderOriginFallbackEnabled = configSnapshot.gateway?.controlUi?.dangerouslyAllowHostHeaderOriginFallback === true;
const originCheck = checkBrowserOrigin({
requestHost,
origin: requestOrigin,
allowedOrigins: configSnapshot.gateway?.controlUi?.allowedOrigins,
allowHostHeaderOriginFallback: hostHeaderOriginFallbackEnabled,
isLocalClient
});
if (!originCheck.ok) {
const errorMessage = "origin not allowed (open the Control UI from the gateway host or allow it in gateway.controlUi.allowedOrigins)";
markHandshakeFailure("origin-mismatch", {
origin: requestOrigin ?? "n/a",
host: requestHost ?? "n/a",
reason: originCheck.reason
});
sendHandshakeErrorResponse(ErrorCodes.INVALID_REQUEST, errorMessage, { details: {
code: ConnectErrorDetailCodes.CONTROL_UI_ORIGIN_NOT_ALLOWED,
reason: originCheck.reason
} });
close(1008, truncateCloseReason(errorMessage));
return;
}
if (originCheck.matchedBy === "host-header-fallback") {
originCheckMetrics.hostHeaderFallbackAccepted += 1;
logWsControl.warn(`security warning: websocket origin accepted via Host-header fallback conn=${connId} count=${originCheckMetrics.hostHeaderFallbackAccepted} host=${requestHost ?? "n/a"} origin=${requestOrigin ?? "n/a"}`);
if (hostHeaderOriginFallbackEnabled) logGateway.warn("security metric: gateway.controlUi.dangerouslyAllowHostHeaderOriginFallback accepted a websocket connect request");
}
}
const deviceRaw = connectParams.device;
let devicePublicKey = null;
let deviceAuthPayloadVersion = null;
const hasTokenAuth = Boolean(connectParams.auth?.token);
const hasPasswordAuth = Boolean(connectParams.auth?.password);
const hasSharedAuth = hasTokenAuth || hasPasswordAuth;
const controlUiAuthPolicy = resolveControlUiAuthPolicy({
isControlUi,
controlUiConfig: configSnapshot.gateway?.controlUi,
deviceRaw
});
const device = controlUiAuthPolicy.device;
let { authResult, authOk, authMethod, sharedAuthOk, bootstrapTokenCandidate, deviceTokenCandidate, deviceTokenCandidateSource } = await resolveConnectAuthState({
resolvedAuth,
connectAuth: connectParams.auth,
hasDeviceIdentity: Boolean(device),
req: upgradeReq,
trustedProxies,
allowRealIpFallback,
rateLimiter: authRateLimiter,
clientIp: browserRateLimitClientIp
});
const rejectUnauthorized = (failedAuth) => {
const { authProvided, canRetryWithDeviceToken, recommendedNextStep } = resolveUnauthorizedHandshakeContext({
connectAuth: connectParams.auth,
failedAuth,
hasDeviceIdentity: Boolean(device)
});
markHandshakeFailure("unauthorized", {
authMode: resolvedAuth.mode,
authProvided,
authReason: failedAuth.reason,
allowTailscale: resolvedAuth.allowTailscale
});
logWsControl.warn(`unauthorized conn=${connId} remote=${remoteAddr ?? "?"} client=${clientLabel} ${connectParams.client.mode} v${connectParams.client.version} reason=${failedAuth.reason ?? "unknown"}`);
const authMessage = formatGatewayAuthFailureMessage({
authMode: resolvedAuth.mode,
authProvided,
reason: failedAuth.reason,
client: connectParams.client
});
sendHandshakeErrorResponse(ErrorCodes.INVALID_REQUEST, authMessage, { details: {
code: resolveAuthConnectErrorDetailCode(failedAuth.reason),
authReason: failedAuth.reason,
canRetryWithDeviceToken,
recommendedNextStep
} });
close(1008, truncateCloseReason(authMessage));
};
const clearUnboundScopes = () => {
if (scopes.length > 0) {
scopes = [];
connectParams.scopes = scopes;
}
};
const handleMissingDeviceIdentity = () => {
const trustedProxyAuthOk = isTrustedProxyControlUiOperatorAuth({
isControlUi,
role,
authMode: resolvedAuth.mode,
authOk,
authMethod
});
const preserveInsecureLocalControlUiScopes = isControlUi && controlUiAuthPolicy.allowInsecureAuthConfigured && isLocalClient && (authMethod === "token" || authMethod === "password");
const decision = evaluateMissingDeviceIdentity({
hasDeviceIdentity: Boolean(device),
role,
isControlUi,
controlUiAuthPolicy,
trustedProxyAuthOk,
sharedAuthOk,
authOk,
hasSharedAuth,
isLocalClient
});
if (!device && (decision.kind !== "allow" || !controlUiAuthPolicy.allowBypass && !preserveInsecureLocalControlUiScopes && (authMethod === "token" || authMethod === "password" || trustedProxyAuthOk))) clearUnboundScopes();
if (decision.kind === "allow") return true;
if (decision.kind === "reject-control-ui-insecure-auth") {
const errorMessage = "control ui requires device identity (use HTTPS or localhost secure context)";
markHandshakeFailure("control-ui-insecure-auth", { insecureAuthConfigured: controlUiAuthPolicy.allowInsecureAuthConfigured });
sendHandshakeErrorResponse(ErrorCodes.INVALID_REQUEST, errorMessage, { details: { code: ConnectErrorDetailCodes.CONTROL_UI_DEVICE_IDENTITY_REQUIRED } });
close(1008, errorMessage);
return false;
}
if (decision.kind === "reject-unauthorized") {
rejectUnauthorized(authResult);
return false;
}
markHandshakeFailure("device-required");
sendHandshakeErrorResponse(ErrorCodes.NOT_PAIRED, "device identity required", { details: { code: ConnectErrorDetailCodes.DEVICE_IDENTITY_REQUIRED } });
close(1008, "device identity required");
return false;
};
if (!handleMissingDeviceIdentity()) return;
if (device) {
const rejectDeviceAuthInvalid = (reason, message) => {
setHandshakeState("failed");
setCloseCause("device-auth-invalid", {
reason,
client: connectParams.client.id,
deviceId: device.id
});
send({
type: "res",
id: frame.id,
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, message, { details: {
code: resolveDeviceAuthConnectErrorDetailCode(reason),
reason
} })
});
close(1008, message);
};
const derivedId = deriveDeviceIdFromPublicKey(device.publicKey);
if (!derivedId || derivedId !== device.id) {
rejectDeviceAuthInvalid("device-id-mismatch", "device identity mismatch");
return;
}
const signedAt = device.signedAt;
if (typeof signedAt !== "number" || Math.abs(Date.now() - signedAt) > DEVICE_SIGNATURE_SKEW_MS) {
rejectDeviceAuthInvalid("device-signature-stale", "device signature expired");
return;
}
const providedNonce = typeof device.nonce === "string" ? device.nonce.trim() : "";
if (!providedNonce) {
rejectDeviceAuthInvalid("device-nonce-missing", "device nonce required");
return;
}
if (providedNonce !== connectNonce) {
rejectDeviceAuthInvalid("device-nonce-mismatch", "device nonce mismatch");
return;
}
const rejectDeviceSignatureInvalid = () => rejectDeviceAuthInvalid("device-signature", "device signature invalid");
const payloadVersion = resolveDeviceSignaturePayloadVersion({
device,
connectParams,
role,
scopes,
signedAtMs: signedAt,
nonce: providedNonce
});
if (!payloadVersion) {
rejectDeviceSignatureInvalid();
return;
}
deviceAuthPayloadVersion = payloadVersion;
devicePublicKey = normalizeDevicePublicKeyBase64Url(device.publicKey);
if (!devicePublicKey) {
rejectDeviceAuthInvalid("device-public-key", "device public key invalid");
return;
}
}
({authResult, authOk, authMethod} = await resolveConnectAuthDecision({
state: {
authResult,
authOk,
authMethod,
sharedAuthOk,
sharedAuthProvided: hasSharedAuth,
bootstrapTokenCandidate,
deviceTokenCandidate,
deviceTokenCandidateSource
},
hasDeviceIdentity: Boolean(device),
deviceId: device?.id,
publicKey: device?.publicKey,
role,
scopes,
rateLimiter: authRateLimiter,
clientIp: browserRateLimitClientIp,
verifyBootstrapToken: async ({ deviceId, publicKey, token, role, scopes }) => await verifyDeviceBootstrapToken({
deviceId,
publicKey,
token,
role,
scopes
}),
verifyDeviceToken
}));
if (!authOk) {
rejectUnauthorized(authResult);
return;
}
const trustedProxyAuthOk = isTrustedProxyControlUiOperatorAuth({
isControlUi,
role,
authMode: resolvedAuth.mode,
authOk,
authMethod
});
const skipPairing = shouldSkipBackendSelfPairing({
connectParams,
isLocalClient,
hasBrowserOriginHeader,
sharedAuthOk,
authMethod
}) || shouldSkipControlUiPairing(controlUiAuthPolicy, role, trustedProxyAuthOk, resolvedAuth.mode);
if (device && devicePublicKey && !skipPairing) {
const formatAuditList = (items) => {
if (!items || items.length === 0) return "<none>";
const out = /* @__PURE__ */ new Set();
for (const item of items) {
const trimmed = item.trim();
if (trimmed) out.add(trimmed);
}
if (out.size === 0) return "<none>";
return [...out].toSorted().join(",");
};
const logUpgradeAudit = (reason, currentRoles, currentScopes) => {
logGateway.warn(`security audit: device access upgrade requested reason=${reason} device=${device.id} ip=${reportedClientIp ?? "unknown-ip"} auth=${authMethod} roleFrom=${formatAuditList(currentRoles)} roleTo=${role} scopesFrom=${formatAuditList(currentScopes)} scopesTo=${formatAuditList(scopes)} client=${connectParams.client.id} conn=${connId}`);
};
const clientPairingMetadata = {
displayName: connectParams.client.displayName,
platform: connectParams.client.platform,
deviceFamily: connectParams.client.deviceFamily,
clientId: connectParams.client.id,
clientMode: connectParams.client.mode,
role,
scopes,
remoteIp: reportedClientIp
};
const clientAccessMetadata = {
displayName: connectParams.client.displayName,
clientId: connectParams.client.id,
clientMode: connectParams.client.mode,
role,
scopes,
remoteIp: reportedClientIp
};
const requirePairing = async (reason) => {
const allowSilentLocalPairing = shouldAllowSilentLocalPairing({
isLocalClient,
hasBrowserOriginHeader,
isControlUi,
isWebchat,
reason
});
const pairing = await requestDevicePairing({
deviceId: device.id,
publicKey: devicePublicKey,
...clientPairingMetadata,
silent: allowSilentLocalPairing
});
const context = buildRequestContext();
if (pairing.request.silent === true) {
const approved = await approveDevicePairing(pairing.request.requestId);
if (approved) {
logGateway.info(`device pairing auto-approved device=${approved.device.deviceId} role=${approved.device.role ?? "unknown"}`);
context.broadcast("device.pair.resolved", {
requestId: pairing.request.requestId,
deviceId: approved.device.deviceId,
decision: "approved",
ts: Date.now()
}, { dropIfSlow: true });
}
} else if (pairing.created) context.broadcast("device.pair.requested", pairing.request, { dropIfSlow: true });
if (pairing.request.silent !== true) {
setHandshakeState("failed");
setCloseCause("pairing-required", {
deviceId: device.id,
requestId: pairing.request.requestId,
reason
});
send({
type: "res",
id: frame.id,
ok: false,
error: errorShape(ErrorCodes.NOT_PAIRED, "pairing required", { details: {
code: ConnectErrorDetailCodes.PAIRING_REQUIRED,
requestId: pairing.request.requestId,
reason
} })
});
close(1008, "pairing required");
return false;
}
return true;
};
const paired = await getPairedDevice(device.id);
if (!(paired?.publicKey === devicePublicKey)) {
if (!await requirePairing("not-paired")) return;
} else {
const claimedPlatform = connectParams.client.platform;
const pairedPlatform = paired.platform;
const claimedDeviceFamily = connectParams.client.deviceFamily;
const pairedDeviceFamily = paired.deviceFamily;
const metadataPinning = resolvePinnedClientMetadata({
claimedPlatform,
claimedDeviceFamily,
pairedPlatform,
pairedDeviceFamily
});
const { platformMismatch, deviceFamilyMismatch } = metadataPinning;
if (platformMismatch || deviceFamilyMismatch) {
logGateway.warn(`security audit: device metadata upgrade requested reason=metadata-upgrade device=${device.id} ip=${reportedClientIp ?? "unknown-ip"} auth=${authMethod} payload=${deviceAuthPayloadVersion ?? "unknown"} claimedPlatform=${claimedPlatform ?? "<none>"} pinnedPlatform=${pairedPlatform ?? "<none>"} claimedDeviceFamily=${claimedDeviceFamily ?? "<none>"} pinnedDeviceFamily=${pairedDeviceFamily ?? "<none>"} client=${connectParams.client.id} conn=${connId}`);
if (!await requirePairing("metadata-upgrade")) return;
} else {
if (metadataPinning.pinnedPlatform) connectParams.client.platform = metadataPinning.pinnedPlatform;
if (metadataPinning.pinnedDeviceFamily) connectParams.client.deviceFamily = metadataPinning.pinnedDeviceFamily;
}
const pairedRoles = Array.isArray(paired.roles) ? paired.roles : paired.role ? [paired.role] : [];
const pairedScopes = Array.isArray(paired.scopes) ? paired.scopes : Array.isArray(paired.approvedScopes) ? paired.approvedScopes : [];
const allowedRoles = new Set(pairedRoles);
if (allowedRoles.size === 0) {
logUpgradeAudit("role-upgrade", pairedRoles, pairedScopes);
if (!await requirePairing("role-upgrade")) return;
} else if (!allowedRoles.has(role)) {
logUpgradeAudit("role-upgrade", pairedRoles, pairedScopes);
if (!await requirePairing("role-upgrade")) return;
}
if (scopes.length > 0) {
if (pairedScopes.length === 0) {
logUpgradeAudit("scope-upgrade", pairedRoles, pairedScopes);
if (!await requirePairing("scope-upgrade")) return;
} else if (!roleScopesAllow({
role,
requestedScopes: scopes,
allowedScopes: pairedScopes
})) {
logUpgradeAudit("scope-upgrade", pairedRoles, pairedScopes);
if (!await requirePairing("scope-upgrade")) return;
}
}
await updatePairedDeviceMetadata(device.id, clientAccessMetadata);
}
}
const deviceToken = device ? await ensureDeviceToken({
deviceId: device.id,
role,
scopes
}) : null;
if (role === "node") {
const allowlist = resolveNodeCommandAllowlist(loadConfig(), {
platform: connectParams.client.platform,
deviceFamily: connectParams.client.deviceFamily
});
connectParams.commands = (Array.isArray(connectParams.commands) ? connectParams.commands : []).map((cmd) => cmd.trim()).filter((cmd) => cmd.length > 0 && allowlist.has(cmd));
}
const shouldTrackPresence = !isGatewayCliClient(connectParams.client);
const clientId = connectParams.client.id;
const instanceId = connectParams.client.instanceId;
const presenceKey = shouldTrackPresence ? device?.id ?? instanceId ?? connId : void 0;
logWs("in", "connect", {
connId,
client: connectParams.client.id,
clientDisplayName: connectParams.client.displayName,
version: connectParams.client.version,
mode: connectParams.client.mode,
clientId,
platform: connectParams.client.platform,
auth: authMethod
});
if (isWebchatConnect(connectParams)) logWsControl.info(`webchat connected conn=${connId} remote=${remoteAddr ?? "?"} client=${clientLabel} ${connectParams.client.mode} v${connectParams.client.version}`);
if (presenceKey) {
upsertPresence(presenceKey, {
host: connectParams.client.displayName ?? connectParams.client.id ?? os.hostname(),
ip: isLocalClient ? void 0 : reportedClientIp,
version: connectParams.client.version,
platform: connectParams.client.platform,
deviceFamily: connectParams.client.deviceFamily,
modelIdentifier: connectParams.client.modelIdentifier,
mode: connectParams.client.mode,
deviceId: device?.id,
roles: [role],
scopes,
instanceId: device?.id ?? instanceId,
reason: "connect"
});
incrementPresenceVersion();
}
const snapshot = buildGatewaySnapshot();
const cachedHealth = getHealthCache();
if (cachedHealth) {
snapshot.health = cachedHealth;
snapshot.stateVersion.health = getHealthVersion();
}
const canvasCapability = role === "node" && canvasHostUrl ? mintCanvasCapabilityToken() : void 0;
const canvasCapabilityExpiresAtMs = canvasCapability ? Date.now() + CANVAS_CAPABILITY_TTL_MS : void 0;
const scopedCanvasHostUrl = canvasHostUrl && canvasCapability ? buildCanvasScopedHostUrl(canvasHostUrl, canvasCapability) ?? canvasHostUrl : canvasHostUrl;
const helloOk = {
type: "hello-ok",
protocol: 3,
server: {
version: resolveRuntimeServiceVersion(process.env),
connId
},
features: {
methods: gatewayMethods,
events
},
snapshot,
canvasHostUrl: scopedCanvasHostUrl,
auth: deviceToken ? {
deviceToken: deviceToken.token,
role: deviceToken.role,
scopes: deviceToken.scopes,
issuedAtMs: deviceToken.rotatedAtMs ?? deviceToken.createdAtMs
} : void 0,
policy: {
maxPayload: MAX_PAYLOAD_BYTES,
maxBufferedBytes: MAX_BUFFERED_BYTES,
tickIntervalMs: TICK_INTERVAL_MS
}
};
clearHandshakeTimer();
const nextClient = {
socket,
connect: connectParams,
connId,
presenceKey,
clientIp: reportedClientIp,
canvasHostUrl,
canvasCapability,
canvasCapabilityExpiresAtMs
};
setSocketMaxPayload(socket, MAX_PAYLOAD_BYTES);
setClient(nextClient);
setHandshakeState("connected");
if (role === "node") {
const context = buildRequestContext();
const nodeSession = context.nodeRegistry.register(nextClient, { remoteIp: reportedClientIp });
const instanceIdRaw = connectParams.client.instanceId;
const instanceId = typeof instanceIdRaw === "string" ? instanceIdRaw.trim() : "";
const nodeIdsForPairing = new Set([nodeSession.nodeId]);
if (instanceId) nodeIdsForPairing.add(instanceId);
for (const nodeId of nodeIdsForPairing) updatePairedNodeMetadata(nodeId, { lastConnectedAtMs: nodeSession.connectedAtMs }).catch((err) => logGateway.warn(`failed to record last connect for ${nodeId}: ${formatForLog(err)}`));
recordRemoteNodeInfo({
nodeId: nodeSession.nodeId,
displayName: nodeSession.displayName,
platform: nodeSession.platform,
deviceFamily: nodeSession.deviceFamily,
commands: nodeSession.commands,
remoteIp: nodeSession.remoteIp
});
refreshRemoteNodeBins({
nodeId: nodeSession.nodeId,
platform: nodeSession.platform,
deviceFamily: nodeSession.deviceFamily,
commands: nodeSession.commands,
cfg: loadConfig()
}).catch((err) => logGateway.warn(`remote bin probe failed for ${nodeSession.nodeId}: ${formatForLog(err)}`));
loadVoiceWakeConfig().then((cfg) => {
context.nodeRegistry.sendEvent(nodeSession.nodeId, "voicewake.changed", { triggers: cfg.triggers });
}).catch((err) => logGateway.warn(`voicewake snapshot failed for ${nodeSession.nodeId}: ${formatForLog(err)}`));
}
logWs("out", "hello-ok", {
connId,
methods: gatewayMethods.length,
events: events.length,
presence: snapshot.presence.length,
stateVersion: snapshot.stateVersion.presence
});
send({
type: "res",
id: frame.id,
ok: true,
payload: helloOk
});
refreshGatewayHealthSnapshot({ probe: true }).catch((err) => logHealth.error(`post-connect health refresh failed: ${formatError(err)}`));
return;
}
if (!validateRequestFrame(parsed)) {
send({
type: "res",
id: parsed?.id ?? "invalid",
ok: false,
error: errorShape(ErrorCodes.INVALID_REQUEST, `invalid request frame: ${formatValidationErrors(validateRequestFrame.errors)}`)
});
return;
}
const req = parsed;
logWs("in", "req", {
connId,
id: req.id,
method: req.method
});
const respond = (ok, payload, error, meta) => {
send({
type: "res",
id: req.id,
ok,
payload,
error
});
const unauthorizedRoleError = isUnauthorizedRoleError(error);
let logMeta = meta;
if (unauthorizedRoleError) {
const unauthorizedDecision = unauthorizedFloodGuard.registerUnauthorized();
if (unauthorizedDecision.suppressedSinceLastLog > 0) logMeta = {
...logMeta,
suppressedUnauthorizedResponses: unauthorizedDecision.suppressedSinceLastLog
};
if (!unauthorizedDecision.shouldLog) return;
if (unauthorizedDecision.shouldClose) {
setCloseCause("repeated-unauthorized-requests", {
unauthorizedCount: unauthorizedDecision.count,
method: req.method
});
queueMicrotask(() => close(1008, "repeated unauthorized calls"));
}
logMeta = {
...logMeta,
unauthorizedCount: unauthorizedDecision.count
};
} else unauthorizedFloodGuard.reset();
logWs("out", "res", {
connId,
id: req.id,
ok,
method: req.method,
errorCode: error?.code,
errorMessage: error?.message,
...logMeta
});
};
(async () => {
await handleGatewayRequest({
req,
respond,
client,
isWebchatConnect,
extraHandlers,
context: buildRequestContext()
});
})().catch((err) => {
logGateway.error(`request handler failed: ${formatForLog(err)}`);
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, formatForLog(err)));
});
} catch (err) {
logGateway.error(`parse/handle error: ${String(err)}`);
logWs("out", "parse-error", {
connId,
error: formatForLog(err)
});
if (!getClient()) close();
}
});
}
function getRawDataByteLength(data) {
if (Buffer.isBuffer(data)) return data.byteLength;
if (Array.isArray(data)) return data.reduce((total, chunk) => total + chunk.byteLength, 0);
if (data instanceof ArrayBuffer) return data.byteLength;
return Buffer.byteLength(String(data));
}
function setSocketMaxPayload(socket, maxPayload) {
const receiver = socket._receiver;
if (receiver) receiver._maxPayload = maxPayload;
}
//#endregion
//#region src/gateway/server/ws-connection.ts
const LOG_HEADER_MAX_LEN = 300;
const LOG_HEADER_FORMAT_REGEX = /\p{Cf}/gu;
function replaceControlChars(value) {
let cleaned = "";
for (const char of value) {
const codePoint = char.codePointAt(0);
if (codePoint !== void 0 && (codePoint <= 31 || codePoint >= 127 && codePoint <= 159)) {
cleaned += " ";
continue;
}
cleaned += char;
}
return cleaned;
}
const sanitizeLogValue = (value) => {
if (!value) return;
const cleaned = replaceControlChars(value).replace(LOG_HEADER_FORMAT_REGEX, " ").replace(/\s+/g, " ").trim();
if (!cleaned) return;
if (cleaned.length <= LOG_HEADER_MAX_LEN) return cleaned;
return truncateUtf16Safe(cleaned, LOG_HEADER_MAX_LEN);
};
function attachGatewayWsConnectionHandler(params) {
const { wss, clients, port, gatewayHost, canvasHostEnabled, canvasHostServerPort, resolvedAuth, rateLimiter, browserRateLimiter, gatewayMethods, events, logGateway, logHealth, logWsControl, extraHandlers, broadcast, buildRequestContext } = params;
const originCheckMetrics = { hostHeaderFallbackAccepted: 0 };
wss.on("connection", (socket, upgradeReq) => {
let client = null;
let closed = false;
const openedAt = Date.now();
const connId = randomUUID();
const remoteAddr = socket._socket?.remoteAddress;
const headerValue = (value) => Array.isArray(value) ? value[0] : value;
const requestHost = headerValue(upgradeReq.headers.host);
const requestOrigin = headerValue(upgradeReq.headers.origin);
const requestUserAgent = headerValue(upgradeReq.headers["user-agent"]);
const forwardedFor = headerValue(upgradeReq.headers["x-forwarded-for"]);
const realIp = headerValue(upgradeReq.headers["x-real-ip"]);
const canvasHostUrl = resolveCanvasHostUrl({
canvasPort: canvasHostServerPort ?? (canvasHostEnabled ? port : void 0),
hostOverride: canvasHostServerPort ? gatewayHost && gatewayHost !== "0.0.0.0" && gatewayHost !== "::" ? gatewayHost : void 0 : void 0,
requestHost: upgradeReq.headers.host,
forwardedProto: upgradeReq.headers["x-forwarded-proto"],
localAddress: upgradeReq.socket?.localAddress
});
logWs("in", "open", {
connId,
remoteAddr
});
let handshakeState = "pending";
let closeCause;
let closeMeta = {};
let lastFrameType;
let lastFrameMethod;
let lastFrameId;
const setCloseCause = (cause, meta) => {
if (!closeCause) closeCause = cause;
if (meta && Object.keys(meta).length > 0) closeMeta = {
...closeMeta,
...meta
};
};
const setLastFrameMeta = (meta) => {
if (meta.type || meta.method || meta.id) {
lastFrameType = meta.type ?? lastFrameType;
lastFrameMethod = meta.method ?? lastFrameMethod;
lastFrameId = meta.id ?? lastFrameId;
}
};
const send = (obj) => {
try {
socket.send(JSON.stringify(obj));
} catch {}
};
const connectNonce = randomUUID();
send({
type: "event",
event: "connect.challenge",
payload: {
nonce: connectNonce,
ts: Date.now()
}
});
const close = (code = 1e3, reason) => {
if (closed) return;
closed = true;
clearTimeout(handshakeTimer);
if (client) clients.delete(client);
try {
socket.close(code, reason);
} catch {}
};
socket.once("error", (err) => {
logWsControl.warn(`error conn=${connId} remote=${remoteAddr ?? "?"}: ${formatError(err)}`);
close();
});
const isNoisySwiftPmHelperClose = (userAgent, remote) => Boolean(userAgent?.toLowerCase().includes("swiftpm-testing-helper") && isLoopbackAddress(remote));
socket.once("close", (code, reason) => {
const durationMs = Date.now() - openedAt;
const logForwardedFor = sanitizeLogValue(forwardedFor);
const logOrigin = sanitizeLogValue(requestOrigin);
const logHost = sanitizeLogValue(requestHost);
const logUserAgent = sanitizeLogValue(requestUserAgent);
const logReason = sanitizeLogValue(reason?.toString());
const closeContext = {
cause: closeCause,
handshake: handshakeState,
durationMs,
lastFrameType,
lastFrameMethod,
lastFrameId,
host: logHost,
origin: logOrigin,
userAgent: logUserAgent,
forwardedFor: logForwardedFor,
...closeMeta
};
if (!client) (isNoisySwiftPmHelperClose(requestUserAgent, remoteAddr) ? logWsControl.debug : logWsControl.warn)(`closed before connect conn=${connId} remote=${remoteAddr ?? "?"} fwd=${logForwardedFor || "n/a"} origin=${logOrigin || "n/a"} host=${logHost || "n/a"} ua=${logUserAgent || "n/a"} code=${code ?? "n/a"} reason=${logReason || "n/a"}`, closeContext);
if (client && isWebchatClient(client.connect.client)) logWsControl.info(`webchat disconnected code=${code} reason=${logReason || "n/a"} conn=${connId}`);
if (client?.presenceKey) {
upsertPresence(client.presenceKey, { reason: "disconnect" });
broadcastPresenceSnapshot({
broadcast,
incrementPresenceVersion,
getHealthVersion
});
}
const context = buildRequestContext();
context.unsubscribeAllSessionEvents(connId);
if (client?.connect?.role === "node") {
const nodeId = context.nodeRegistry.unregister(connId);
if (nodeId) {
removeRemoteNodeInfo(nodeId);
context.nodeUnsubscribeAll(nodeId);
}
}
logWs("out", "close", {
connId,
code,
reason: logReason,
durationMs,
cause: closeCause,
handshake: handshakeState,
lastFrameType,
lastFrameMethod,
lastFrameId
});
close();
});
const handshakeTimeoutMs = getHandshakeTimeoutMs();
const handshakeTimer = setTimeout(() => {
if (!client) {
handshakeState = "failed";
setCloseCause("handshake-timeout", { handshakeMs: Date.now() - openedAt });
logWsControl.warn(`handshake timeout conn=${connId} remote=${remoteAddr ?? "?"}`);
close();
}
}, handshakeTimeoutMs);
attachGatewayWsMessageHandler({
socket,
upgradeReq,
connId,
remoteAddr,
forwardedFor,
realIp,
requestHost,
requestOrigin,
requestUserAgent,
canvasHostUrl,
connectNonce,
resolvedAuth,
rateLimiter,
browserRateLimiter,
gatewayMethods,
events,
extraHandlers,
buildRequestContext,
send,
close,
isClosed: () => closed,
clearHandshakeTimer: () => clearTimeout(handshakeTimer),
getClient: () => client,
setClient: (next) => {
client = next;
clients.add(next);
},
setHandshakeState: (next) => {
handshakeState = next;
},
setCloseCause,
setLastFrameMeta,
originCheckMetrics,
logGateway,
logHealth,
logWsControl
});
});
}
//#endregion
//#region src/gateway/server-ws-runtime.ts
function attachGatewayWsHandlers(params) {
attachGatewayWsConnectionHandler({
wss: params.wss,
clients: params.clients,
port: params.port,
gatewayHost: params.gatewayHost,
canvasHostEnabled: params.canvasHostEnabled,
canvasHostServerPort: params.canvasHostServerPort,
resolvedAuth: params.resolvedAuth,
rateLimiter: params.rateLimiter,
browserRateLimiter: params.browserRateLimiter,
gatewayMethods: params.gatewayMethods,
events: params.events,
logGateway: params.logGateway,
logHealth: params.logHealth,
logWsControl: params.logWsControl,
extraHandlers: params.extraHandlers,
broadcast: params.broadcast,
buildRequestContext: () => params.context
});
}
//#endregion
//#region src/gateway/server/readiness.ts
const DEFAULT_READINESS_CACHE_TTL_MS = 1e3;
function shouldIgnoreReadinessFailure(accountSnapshot, health) {
if (health.reason === "unmanaged" || health.reason === "stale-socket") return true;
return health.reason === "not-running" && accountSnapshot.restartPending === true;
}
function createReadinessChecker(deps) {
const { channelManager, startedAt } = deps;
const cacheTtlMs = Math.max(0, deps.cacheTtlMs ?? DEFAULT_READINESS_CACHE_TTL_MS);
let cachedAt = 0;
let cachedState = null;
return () => {
const now = Date.now();
const uptimeMs = now - startedAt;
if (cachedState && now - cachedAt < cacheTtlMs) return {
...cachedState,
uptimeMs
};
const snapshot = channelManager.getRuntimeSnapshot();
const failing = [];
for (const [channelId, accounts] of Object.entries(snapshot.channelAccounts)) {
if (!accounts) continue;
for (const accountSnapshot of Object.values(accounts)) {
if (!accountSnapshot) continue;
const health = evaluateChannelHealth(accountSnapshot, {
now,
staleEventThresholdMs: DEFAULT_CHANNEL_STALE_EVENT_THRESHOLD_MS,
channelConnectGraceMs: DEFAULT_CHANNEL_CONNECT_GRACE_MS,
channelId
});
if (!health.healthy && !shouldIgnoreReadinessFailure(accountSnapshot, health)) {
failing.push(channelId);
break;
}
}
}
cachedAt = now;
cachedState = {
ready: failing.length === 0,
failing
};
return {
...cachedState,
uptimeMs
};
};
}
//#endregion
//#region src/gateway/server/tls.ts
async function loadGatewayTlsRuntime(cfg, log) {
return await loadGatewayTlsRuntime$1(cfg, log);
}
//#endregion
//#region src/gateway/session-transcript-key.ts
const TRANSCRIPT_SESSION_KEY_CACHE = /* @__PURE__ */ new Map();
function resolveTranscriptPathForComparison(value) {
const trimmed = value?.trim();
if (!trimmed) return;
const resolved = path.resolve(trimmed);
try {
return fs.realpathSync(resolved);
} catch {
return resolved;
}
}
function sessionKeyMatchesTranscriptPath(params) {
const entry = params.store[params.key];
if (!entry?.sessionId) return false;
const target = resolveGatewaySessionStoreTarget({
cfg: params.cfg,
key: params.key,
scanLegacyKeys: false,
store: params.store
});
const sessionAgentId = normalizeAgentId(target.agentId);
return resolveSessionTranscriptCandidates(entry.sessionId, target.storePath, entry.sessionFile, sessionAgentId).some((candidate) => resolveTranscriptPathForComparison(candidate) === params.targetPath);
}
function resolveSessionKeyForTranscriptFile(sessionFile) {
const targetPath = resolveTranscriptPathForComparison(sessionFile);
if (!targetPath) return;
const cfg = loadConfig();
const { store } = loadCombinedSessionStoreForGateway(cfg);
const cachedKey = TRANSCRIPT_SESSION_KEY_CACHE.get(targetPath);
if (cachedKey && sessionKeyMatchesTranscriptPath({
cfg,
store,
key: cachedKey,
targetPath
})) return cachedKey;
for (const [key, entry] of Object.entries(store)) {
if (!entry?.sessionId || key === cachedKey) continue;
if (sessionKeyMatchesTranscriptPath({
cfg,
store,
key,
targetPath
})) {
TRANSCRIPT_SESSION_KEY_CACHE.set(targetPath, key);
return key;
}
}
TRANSCRIPT_SESSION_KEY_CACHE.delete(targetPath);
}
//#endregion
//#region src/gateway/startup-control-ui-origins.ts
async function maybeSeedControlUiAllowedOriginsAtStartup(params) {
const seeded = ensureControlUiAllowedOriginsForNonLoopbackBind(params.config);
if (!seeded.seededOrigins || !seeded.bind) return params.config;
try {
await params.writeConfig(seeded.config);
params.log.info(buildSeededOriginsInfoLog(seeded.seededOrigins, seeded.bind));
} catch (err) {
params.log.warn(`gateway: failed to persist gateway.controlUi.allowedOrigins seed: ${String(err)}. The gateway will start with the in-memory value but config was not saved.`);
}
return seeded.config;
}
function buildSeededOriginsInfoLog(origins, bind) {
return `gateway: seeded gateway.controlUi.allowedOrigins ${JSON.stringify(origins)} for bind=${bind} (required since v2026.2.26; see issue #29385). Add other origins to gateway.controlUi.allowedOrigins if needed.`;
}
//#endregion
//#region src/gateway/server.impl.ts
ensureOpenClawCliOnPath();
const MAX_MEDIA_TTL_HOURS = 168;
function resolveMediaCleanupTtlMs(ttlHoursRaw) {
const ttlMs = Math.min(Math.max(ttlHoursRaw, 1), MAX_MEDIA_TTL_HOURS) * 60 * 6e4;
if (!Number.isFinite(ttlMs) || !Number.isSafeInteger(ttlMs)) throw new Error(`Invalid media.ttlHours: ${String(ttlHoursRaw)}`);
return ttlMs;
}
const log = createSubsystemLogger("gateway");
const logCanvas = log.child("canvas");
const logDiscovery = log.child("discovery");
const logTailscale = log.child("tailscale");
const logChannels = log.child("channels");
const logBrowser = log.child("browser");
let cachedChannelRuntime = null;
function getChannelRuntime() {
cachedChannelRuntime ??= createPluginRuntime().channel;
return cachedChannelRuntime;
}
const logHealth = log.child("health");
const logCron = log.child("cron");
const logReload = log.child("reload");
const logHooks = log.child("hooks");
const logPlugins = log.child("plugins");
const logWsControl = log.child("ws");
const logSecrets = log.child("secrets");
const gatewayRuntime = runtimeForLogger(log);
const canvasRuntime = runtimeForLogger(logCanvas);
function createGatewayAuthRateLimiters(rateLimitConfig) {
return {
rateLimiter: rateLimitConfig ? createAuthRateLimiter(rateLimitConfig) : void 0,
browserRateLimiter: createAuthRateLimiter({
...rateLimitConfig,
exemptLoopback: false
})
};
}
function logGatewayAuthSurfaceDiagnostics(prepared) {
const states = evaluateGatewayAuthSurfaceStates({
config: prepared.sourceConfig,
defaults: prepared.sourceConfig.secrets?.defaults,
env: process.env
});
const inactiveWarnings = /* @__PURE__ */ new Map();
for (const warning of prepared.warnings) {
if (warning.code !== "SECRETS_REF_IGNORED_INACTIVE_SURFACE") continue;
inactiveWarnings.set(warning.path, warning.message);
}
for (const path of GATEWAY_AUTH_SURFACE_PATHS) {
const state = states[path];
if (!state.hasSecretRef) continue;
const stateLabel = state.active ? "active" : "inactive";
const details = (!state.active && inactiveWarnings.get(path) ? inactiveWarnings.get(path) : void 0) ?? state.reason;
logSecrets.info(`[SECRETS_GATEWAY_AUTH_SURFACE] ${path} is ${stateLabel}. ${details}`);
}
}
function applyGatewayAuthOverridesForStartupPreflight(config, overrides) {
if (!overrides.auth && !overrides.tailscale) return config;
return {
...config,
gateway: {
...config.gateway,
auth: mergeGatewayAuthConfig(config.gateway?.auth, overrides.auth),
tailscale: mergeGatewayTailscaleConfig(config.gateway?.tailscale, overrides.tailscale)
}
};
}
function assertValidGatewayStartupConfigSnapshot(snapshot, options = {}) {
if (snapshot.valid) return;
const issues = snapshot.issues.length > 0 ? formatConfigIssueLines(snapshot.issues, "", { normalizeRoot: true }).join("\n") : "Unknown validation issue.";
const doctorHint = options.includeDoctorHint ? `\nRun "${formatCliCommand("openclaw doctor")}" to repair, then retry.` : "";
throw new Error(`Invalid config at ${snapshot.path}.\n${issues}${doctorHint}`);
}
async function prepareGatewayStartupConfig(params) {
assertValidGatewayStartupConfigSnapshot(params.configSnapshot);
const startupPreflightConfig = applyGatewayAuthOverridesForStartupPreflight(params.runtimeConfig, {
auth: params.authOverride,
tailscale: params.tailscaleOverride
});
await params.activateRuntimeSecrets(startupPreflightConfig, {
reason: "startup",
activate: false
});
const authBootstrap = await ensureGatewayStartupAuth({
cfg: params.runtimeConfig,
env: process.env,
authOverride: params.authOverride,
tailscaleOverride: params.tailscaleOverride,
persist: true
});
const runtimeStartupConfig = applyGatewayAuthOverridesForStartupPreflight(authBootstrap.cfg, {
auth: params.authOverride,
tailscale: params.tailscaleOverride
});
const activatedConfig = (await params.activateRuntimeSecrets(runtimeStartupConfig, {
reason: "startup",
activate: true
})).config;
return {
...authBootstrap,
cfg: activatedConfig
};
}
async function startGatewayServer(port = 18789, opts = {}) {
const minimalTestGateway = process.env.VITEST === "1" && process.env.OPENCLAW_TEST_MINIMAL_GATEWAY === "1";
process.env.OPENCLAW_GATEWAY_PORT = String(port);
logAcceptedEnvOption({
key: "OPENCLAW_RAW_STREAM",
description: "raw stream logging enabled"
});
logAcceptedEnvOption({
key: "OPENCLAW_RAW_STREAM_PATH",
description: "raw stream log path override"
});
let configSnapshot = await readConfigFileSnapshot();
if (configSnapshot.legacyIssues.length > 0) {
if (isNixMode) throw new Error("Legacy config entries detected while running in Nix mode. Update your Nix config to the latest schema and restart.");
const { config: migrated, changes } = migrateLegacyConfig(configSnapshot.parsed);
if (!migrated) log.warn("gateway: legacy config entries detected but no auto-migration changes were produced; continuing with validation.");
else {
await writeConfigFile(migrated);
if (changes.length > 0) log.info(`gateway: migrated legacy config entries:\n${changes.map((entry) => `- ${entry}`).join("\n")}`);
}
}
configSnapshot = await readConfigFileSnapshot();
if (configSnapshot.exists) assertValidGatewayStartupConfigSnapshot(configSnapshot, { includeDoctorHint: true });
const autoEnable = applyPluginAutoEnable({
config: configSnapshot.config,
env: process.env
});
if (autoEnable.changes.length > 0) try {
await writeConfigFile(autoEnable.config);
configSnapshot = await readConfigFileSnapshot();
assertValidGatewayStartupConfigSnapshot(configSnapshot);
log.info(`gateway: auto-enabled plugins:\n${autoEnable.changes.map((entry) => `- ${entry}`).join("\n")}`);
} catch (err) {
log.warn(`gateway: failed to persist plugin auto-enable changes: ${String(err)}`);
}
let secretsDegraded = false;
const emitSecretsStateEvent = (code, message, cfg) => {
enqueueSystemEvent(`[${code}] ${message}`, {
sessionKey: resolveMainSessionKey(cfg),
contextKey: code
});
};
let secretsActivationTail = Promise.resolve();
const runWithSecretsActivationLock = async (operation) => {
const run = secretsActivationTail.then(operation, operation);
secretsActivationTail = run.then(() => void 0, () => void 0);
return await run;
};
const activateRuntimeSecrets = async (config, params) => await runWithSecretsActivationLock(async () => {
try {
const prepared = await prepareSecretsRuntimeSnapshot({ config });
if (params.activate) {
activateSecretsRuntimeSnapshot(prepared);
logGatewayAuthSurfaceDiagnostics(prepared);
}
for (const warning of prepared.warnings) logSecrets.warn(`[${warning.code}] ${warning.message}`);
if (secretsDegraded) {
const recoveredMessage = "Secret resolution recovered; runtime remained on last-known-good during the outage.";
logSecrets.info(`[SECRETS_RELOADER_RECOVERED] ${recoveredMessage}`);
emitSecretsStateEvent("SECRETS_RELOADER_RECOVERED", recoveredMessage, prepared.config);
}
secretsDegraded = false;
return prepared;
} catch (err) {
const details = String(err);
if (!secretsDegraded) {
logSecrets.error(`[SECRETS_RELOADER_DEGRADED] ${details}`);
if (params.reason !== "startup") emitSecretsStateEvent("SECRETS_RELOADER_DEGRADED", `Secret resolution failed; runtime remains on last-known-good snapshot. ${details}`, config);
} else logSecrets.warn(`[SECRETS_RELOADER_DEGRADED] ${details}`);
secretsDegraded = true;
if (params.reason === "startup") throw new Error(`Startup failed: required secrets are unavailable. ${details}`, { cause: err });
throw err;
}
});
let cfgAtStart;
const startupRuntimeConfig = applyConfigOverrides(configSnapshot.config);
const authBootstrap = await prepareGatewayStartupConfig({
configSnapshot,
runtimeConfig: startupRuntimeConfig,
authOverride: opts.auth,
tailscaleOverride: opts.tailscale,
activateRuntimeSecrets
});
cfgAtStart = authBootstrap.cfg;
if (authBootstrap.generatedToken) if (authBootstrap.persistedGeneratedToken) log.info("Gateway auth token was missing. Generated a new token and saved it to config (gateway.auth.token).");
else log.warn("Gateway auth token was missing. Generated a runtime token for this startup without changing config; restart will generate a different token. Persist one with `openclaw config set gateway.auth.mode token` and `openclaw config set gateway.auth.token <token>`.");
const diagnosticsEnabled = isDiagnosticsEnabled(cfgAtStart);
if (diagnosticsEnabled) startDiagnosticHeartbeat();
setGatewaySigusr1RestartPolicy({ allowExternal: isRestartEnabled(cfgAtStart) });
setPreRestartDeferralCheck(() => getTotalQueueSize() + getTotalPendingReplies() + getActiveEmbeddedRunCount());
cfgAtStart = await maybeSeedControlUiAllowedOriginsAtStartup({
config: cfgAtStart,
writeConfig: writeConfigFile,
log
});
await runStartupMatrixMigration({
cfg: cfgAtStart,
env: process.env,
log
});
const matrixInstallPathIssue = await detectPluginInstallPathIssue({
pluginId: "matrix",
install: cfgAtStart.plugins?.installs?.matrix
});
if (matrixInstallPathIssue) {
const lines = formatPluginInstallPathIssue({
issue: matrixInstallPathIssue,
pluginLabel: "Matrix",
defaultInstallCommand: "openclaw plugins install @openclaw/matrix",
repoInstallCommand: "openclaw plugins install ./extensions/matrix",
formatCommand: formatCliCommand
});
log.warn(`gateway: matrix install path warning:\n${lines.map((entry) => `- ${entry}`).join("\n")}`);
}
initSubagentRegistry();
const defaultAgentId = resolveDefaultAgentId(cfgAtStart);
const defaultWorkspaceDir = resolveAgentWorkspaceDir(cfgAtStart, defaultAgentId);
const deferredConfiguredChannelPluginIds = minimalTestGateway ? [] : resolveConfiguredDeferredChannelPluginIds({
config: cfgAtStart,
workspaceDir: defaultWorkspaceDir,
env: process.env
});
const baseMethods = listGatewayMethods();
let pluginRegistry = createEmptyPluginRegistry();
let baseGatewayMethods = baseMethods;
if (!minimalTestGateway) ({pluginRegistry, gatewayMethods: baseGatewayMethods} = loadGatewayPlugins({
cfg: cfgAtStart,
workspaceDir: defaultWorkspaceDir,
log,
coreGatewayHandlers,
baseMethods,
preferSetupRuntimeForChannelPlugins: deferredConfiguredChannelPluginIds.length > 0
}));
const channelLogs = Object.fromEntries(listChannelPlugins().map((plugin) => [plugin.id, logChannels.child(plugin.id)]));
const channelRuntimeEnvs = Object.fromEntries(Object.entries(channelLogs).map(([id, logger]) => [id, runtimeForLogger(logger)]));
const channelMethods = listChannelPlugins().flatMap((plugin) => plugin.gatewayMethods ?? []);
const gatewayMethods = Array.from(new Set([...baseGatewayMethods, ...channelMethods]));
let pluginServices = null;
const runtimeConfig = await resolveGatewayRuntimeConfig({
cfg: cfgAtStart,
port,
bind: opts.bind,
host: opts.host,
controlUiEnabled: opts.controlUiEnabled,
openAiChatCompletionsEnabled: opts.openAiChatCompletionsEnabled,
openResponsesEnabled: opts.openResponsesEnabled,
auth: opts.auth,
tailscale: opts.tailscale
});
const { bindHost, controlUiEnabled, openAiChatCompletionsEnabled, openAiChatCompletionsConfig, openResponsesEnabled, openResponsesConfig, strictTransportSecurityHeader, controlUiBasePath, controlUiRoot: controlUiRootOverride, resolvedAuth, tailscaleConfig, tailscaleMode } = runtimeConfig;
let hooksConfig = runtimeConfig.hooksConfig;
let hookClientIpConfig = resolveHookClientIpConfig(cfgAtStart);
const canvasHostEnabled = runtimeConfig.canvasHostEnabled;
const rateLimitConfig = cfgAtStart.gateway?.auth?.rateLimit;
const { rateLimiter: authRateLimiter, browserRateLimiter: browserAuthRateLimiter } = createGatewayAuthRateLimiters(rateLimitConfig);
let controlUiRootState;
if (controlUiRootOverride) {
const resolvedOverride = resolveControlUiRootOverrideSync(controlUiRootOverride);
const resolvedOverridePath = path.resolve(controlUiRootOverride);
controlUiRootState = resolvedOverride ? {
kind: "resolved",
path: resolvedOverride
} : {
kind: "invalid",
path: resolvedOverridePath
};
if (!resolvedOverride) log.warn(`gateway: controlUi.root not found at ${resolvedOverridePath}`);
} else if (controlUiEnabled) {
let resolvedRoot = resolveControlUiRootSync({
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
});
if (!resolvedRoot) {
const ensureResult = await ensureControlUiAssetsBuilt(gatewayRuntime);
if (!ensureResult.ok && ensureResult.message) log.warn(`gateway: ${ensureResult.message}`);
resolvedRoot = resolveControlUiRootSync({
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
});
}
controlUiRootState = resolvedRoot ? {
kind: isPackageProvenControlUiRootSync(resolvedRoot, {
moduleUrl: import.meta.url,
argv1: process.argv[1],
cwd: process.cwd()
}) ? "bundled" : "resolved",
path: resolvedRoot
} : { kind: "missing" };
}
const wizardRunner = opts.wizardRunner ?? runSetupWizard;
const { wizardSessions, findRunningWizard, purgeWizardSession } = createWizardSessionTracker();
const deps = createDefaultDeps();
let canvasHostServer = null;
const gatewayTls = await loadGatewayTlsRuntime(cfgAtStart.gateway?.tls, log.child("tls"));
if (cfgAtStart.gateway?.tls?.enabled && !gatewayTls.enabled) throw new Error(gatewayTls.error ?? "gateway tls: failed to enable");
const serverStartedAt = Date.now();
const channelManager = createChannelManager({
loadConfig,
channelLogs,
channelRuntimeEnvs,
resolveChannelRuntime: getChannelRuntime
});
const getReadiness = createReadinessChecker({
channelManager,
startedAt: serverStartedAt
});
const { canvasHost, releasePluginRouteRegistry, httpServer, httpServers, httpBindHosts, wss, clients, broadcast, broadcastToConnIds, agentRunSeq, dedupe, chatRunState, chatRunBuffers, chatDeltaSentAt, chatDeltaLastBroadcastLen, addChatRun, removeChatRun, chatAbortControllers, toolEventRecipients } = await createGatewayRuntimeState({
cfg: cfgAtStart,
bindHost,
port,
controlUiEnabled,
controlUiBasePath,
controlUiRoot: controlUiRootState,
openAiChatCompletionsEnabled,
openAiChatCompletionsConfig,
openResponsesEnabled,
openResponsesConfig,
strictTransportSecurityHeader,
resolvedAuth,
rateLimiter: authRateLimiter,
gatewayTls,
hooksConfig: () => hooksConfig,
getHookClientIpConfig: () => hookClientIpConfig,
pluginRegistry,
deps,
canvasRuntime,
canvasHostEnabled,
allowCanvasHostInTests: opts.allowCanvasHostInTests,
logCanvas,
log,
logHooks,
logPlugins,
getReadiness
});
let bonjourStop = null;
const nodeRegistry = new NodeRegistry();
const nodePresenceTimers = /* @__PURE__ */ new Map();
const nodeSubscriptions = createNodeSubscriptionManager();
const sessionEventSubscribers = createSessionEventSubscriberRegistry();
const sessionMessageSubscribers = createSessionMessageSubscriberRegistry();
const nodeSendEvent = (opts) => {
const payload = safeParseJson(opts.payloadJSON ?? null);
nodeRegistry.sendEvent(opts.nodeId, opts.event, payload);
};
const nodeSendToSession = (sessionKey, event, payload) => nodeSubscriptions.sendToSession(sessionKey, event, payload, nodeSendEvent);
const nodeSendToAllSubscribed = (event, payload) => nodeSubscriptions.sendToAllSubscribed(event, payload, nodeSendEvent);
const nodeSubscribe = nodeSubscriptions.subscribe;
const nodeUnsubscribe = nodeSubscriptions.unsubscribe;
const nodeUnsubscribeAll = nodeSubscriptions.unsubscribeAll;
const broadcastVoiceWakeChanged = (triggers) => {
broadcast("voicewake.changed", { triggers }, { dropIfSlow: true });
};
const hasMobileNodeConnected = () => hasConnectedMobileNode(nodeRegistry);
applyGatewayLaneConcurrency(cfgAtStart);
let cronState = buildGatewayCronService({
cfg: cfgAtStart,
deps,
broadcast
});
let { cron, storePath: cronStorePath } = cronState;
const { getRuntimeSnapshot, startChannels, startChannel, stopChannel, markChannelLoggedOut } = channelManager;
if (!minimalTestGateway) bonjourStop = (await startGatewayDiscovery({
machineDisplayName: await getMachineDisplayName(),
port,
gatewayTls: gatewayTls.enabled ? {
enabled: true,
fingerprintSha256: gatewayTls.fingerprintSha256
} : void 0,
wideAreaDiscoveryEnabled: cfgAtStart.discovery?.wideArea?.enabled === true,
wideAreaDiscoveryDomain: cfgAtStart.discovery?.wideArea?.domain,
tailscaleMode,
mdnsMode: cfgAtStart.discovery?.mdns?.mode,
logDiscovery
})).bonjourStop;
if (!minimalTestGateway) {
setSkillsRemoteRegistry(nodeRegistry);
primeRemoteSkillsCache();
}
let skillsRefreshTimer = null;
const skillsRefreshDelayMs = 3e4;
const skillsChangeUnsub = minimalTestGateway ? () => {} : registerSkillsChangeListener((event) => {
if (event.reason === "remote-node") return;
if (skillsRefreshTimer) clearTimeout(skillsRefreshTimer);
skillsRefreshTimer = setTimeout(() => {
skillsRefreshTimer = null;
refreshRemoteBinsForConnectedNodes(loadConfig());
}, skillsRefreshDelayMs);
});
const noopInterval = () => setInterval(() => {}, 1 << 30);
let tickInterval = noopInterval();
let healthInterval = noopInterval();
let dedupeCleanup = noopInterval();
let mediaCleanup = null;
if (!minimalTestGateway) ({tickInterval, healthInterval, dedupeCleanup, mediaCleanup} = startGatewayMaintenanceTimers({
broadcast,
nodeSendToAllSubscribed,
getPresenceVersion,
getHealthVersion,
refreshGatewayHealthSnapshot,
logHealth,
dedupe,
chatAbortControllers,
chatRunState,
chatRunBuffers,
chatDeltaSentAt,
chatDeltaLastBroadcastLen,
removeChatRun,
agentRunSeq,
nodeSendToSession,
...typeof cfgAtStart.media?.ttlHours === "number" ? { mediaCleanupTtlMs: resolveMediaCleanupTtlMs(cfgAtStart.media.ttlHours) } : {}
}));
const agentUnsub = minimalTestGateway ? null : onAgentEvent(createAgentEventHandler({
broadcast,
broadcastToConnIds,
nodeSendToSession,
agentRunSeq,
chatRunState,
resolveSessionKeyForRun,
clearAgentRunContext,
toolEventRecipients,
sessionEventSubscribers
}));
const heartbeatUnsub = minimalTestGateway ? null : onHeartbeatEvent((evt) => {
broadcast("heartbeat", evt, { dropIfSlow: true });
});
const transcriptUnsub = minimalTestGateway ? null : onSessionTranscriptUpdate((update) => {
const sessionKey = update.sessionKey ?? resolveSessionKeyForTranscriptFile(update.sessionFile);
if (!sessionKey || update.message === void 0) return;
const connIds = /* @__PURE__ */ new Set();
for (const connId of sessionEventSubscribers.getAll()) connIds.add(connId);
for (const connId of sessionMessageSubscribers.get(sessionKey)) connIds.add(connId);
if (connIds.size === 0) return;
const { entry, storePath } = loadSessionEntry(sessionKey);
const messageSeq = entry?.sessionId ? readSessionMessages(entry.sessionId, storePath, entry.sessionFile).length : void 0;
const sessionRow = loadGatewaySessionRow(sessionKey);
const sessionSnapshot = sessionRow ? {
session: sessionRow,
updatedAt: sessionRow.updatedAt ?? void 0,
sessionId: sessionRow.sessionId,
kind: sessionRow.kind,
channel: sessionRow.channel,
label: sessionRow.label,
displayName: sessionRow.displayName,
deliveryContext: sessionRow.deliveryContext,
parentSessionKey: sessionRow.parentSessionKey,
childSessions: sessionRow.childSessions,
thinkingLevel: sessionRow.thinkingLevel,
systemSent: sessionRow.systemSent,
abortedLastRun: sessionRow.abortedLastRun,
lastChannel: sessionRow.lastChannel,
lastTo: sessionRow.lastTo,
lastAccountId: sessionRow.lastAccountId,
totalTokens: sessionRow.totalTokens,
totalTokensFresh: sessionRow.totalTokensFresh,
contextTokens: sessionRow.contextTokens,
estimatedCostUsd: sessionRow.estimatedCostUsd,
modelProvider: sessionRow.modelProvider,
model: sessionRow.model,
status: sessionRow.status,
startedAt: sessionRow.startedAt,
endedAt: sessionRow.endedAt,
runtimeMs: sessionRow.runtimeMs
} : {};
broadcastToConnIds("session.message", {
sessionKey,
message: attachOpenClawTranscriptMeta(update.message, {
...typeof update.messageId === "string" ? { id: update.messageId } : {},
...typeof messageSeq === "number" ? { seq: messageSeq } : {}
}),
...typeof update.messageId === "string" ? { messageId: update.messageId } : {},
...typeof messageSeq === "number" ? { messageSeq } : {},
...sessionSnapshot
}, connIds, { dropIfSlow: true });
const sessionEventConnIds = sessionEventSubscribers.getAll();
if (sessionEventConnIds.size > 0) broadcastToConnIds("sessions.changed", {
sessionKey,
phase: "message",
ts: Date.now(),
...typeof update.messageId === "string" ? { messageId: update.messageId } : {},
...typeof messageSeq === "number" ? { messageSeq } : {},
...sessionSnapshot
}, sessionEventConnIds, { dropIfSlow: true });
});
const lifecycleUnsub = minimalTestGateway ? null : onSessionLifecycleEvent((event) => {
const connIds = sessionEventSubscribers.getAll();
if (connIds.size === 0) return;
const sessionRow = loadGatewaySessionRow(event.sessionKey);
broadcastToConnIds("sessions.changed", {
sessionKey: event.sessionKey,
reason: event.reason,
parentSessionKey: event.parentSessionKey,
label: event.label,
displayName: event.displayName,
ts: Date.now(),
...sessionRow ? {
updatedAt: sessionRow.updatedAt ?? void 0,
sessionId: sessionRow.sessionId,
kind: sessionRow.kind,
channel: sessionRow.channel,
label: event.label ?? sessionRow.label,
displayName: event.displayName ?? sessionRow.displayName,
deliveryContext: sessionRow.deliveryContext,
parentSessionKey: event.parentSessionKey ?? sessionRow.parentSessionKey,
childSessions: sessionRow.childSessions,
thinkingLevel: sessionRow.thinkingLevel,
systemSent: sessionRow.systemSent,
abortedLastRun: sessionRow.abortedLastRun,
lastChannel: sessionRow.lastChannel,
lastTo: sessionRow.lastTo,
lastAccountId: sessionRow.lastAccountId,
totalTokens: sessionRow.totalTokens,
totalTokensFresh: sessionRow.totalTokensFresh,
contextTokens: sessionRow.contextTokens,
estimatedCostUsd: sessionRow.estimatedCostUsd,
modelProvider: sessionRow.modelProvider,
model: sessionRow.model,
status: sessionRow.status,
startedAt: sessionRow.startedAt,
endedAt: sessionRow.endedAt,
runtimeMs: sessionRow.runtimeMs
} : {}
}, connIds, { dropIfSlow: true });
});
let heartbeatRunner = minimalTestGateway ? {
stop: () => {},
updateConfig: () => {}
} : startHeartbeatRunner({ cfg: cfgAtStart });
const healthCheckMinutes = cfgAtStart.gateway?.channelHealthCheckMinutes;
const healthCheckDisabled = healthCheckMinutes === 0;
const staleEventThresholdMinutes = cfgAtStart.gateway?.channelStaleEventThresholdMinutes;
const maxRestartsPerHour = cfgAtStart.gateway?.channelMaxRestartsPerHour;
let channelHealthMonitor = healthCheckDisabled ? null : startChannelHealthMonitor({
channelManager,
checkIntervalMs: (healthCheckMinutes ?? 5) * 6e4,
...staleEventThresholdMinutes != null && { staleEventThresholdMs: staleEventThresholdMinutes * 6e4 },
...maxRestartsPerHour != null && { maxRestartsPerHour }
});
if (!minimalTestGateway) cron.start().catch((err) => logCron.error(`failed to start: ${String(err)}`));
const stopModelPricingRefresh = !minimalTestGateway && process.env.VITEST !== "1" ? startGatewayModelPricingRefresh({ config: cfgAtStart }) : () => {};
if (!minimalTestGateway) (async () => {
const { recoverPendingDeliveries } = await import("./delivery-queue-BETaQABs.js");
const { deliverOutboundPayloads } = await import("./deliver-B_xeQ0kT.js");
await recoverPendingDeliveries({
deliver: deliverOutboundPayloads,
log: log.child("delivery-recovery"),
cfg: cfgAtStart
});
})().catch((err) => log.error(`Delivery recovery failed: ${String(err)}`));
const execApprovalManager = new ExecApprovalManager();
const execApprovalHandlers = createExecApprovalHandlers(execApprovalManager, { forwarder: createExecApprovalForwarder() });
const secretsHandlers = createSecretsHandlers({
reloadSecrets: async () => {
const active = getActiveSecretsRuntimeSnapshot();
if (!active) throw new Error("Secrets runtime snapshot is not active.");
return { warningCount: (await activateRuntimeSecrets(active.sourceConfig, {
reason: "reload",
activate: true
})).warnings.length };
},
resolveSecrets: async ({ commandName, targetIds }) => {
const { assignments, diagnostics, inactiveRefPaths } = resolveCommandSecretsFromActiveRuntimeSnapshot({
commandName,
targetIds: new Set(targetIds)
});
if (assignments.length === 0) return {
assignments: [],
diagnostics,
inactiveRefPaths
};
return {
assignments,
diagnostics,
inactiveRefPaths
};
}
});
const canvasHostServerPort = canvasHostServer?.port;
const gatewayRequestContext = {
deps,
cron,
cronStorePath,
execApprovalManager,
loadGatewayModelCatalog,
getHealthCache,
refreshHealthSnapshot: refreshGatewayHealthSnapshot,
logHealth,
logGateway: log,
incrementPresenceVersion,
getHealthVersion,
broadcast,
broadcastToConnIds,
nodeSendToSession,
nodeSendToAllSubscribed,
nodeSubscribe,
nodeUnsubscribe,
nodeUnsubscribeAll,
hasConnectedMobileNode: hasMobileNodeConnected,
hasExecApprovalClients: () => {
for (const gatewayClient of clients) {
const scopes = Array.isArray(gatewayClient.connect.scopes) ? gatewayClient.connect.scopes : [];
if (scopes.includes("operator.admin") || scopes.includes("operator.approvals")) return true;
}
return false;
},
nodeRegistry,
agentRunSeq,
chatAbortControllers,
chatAbortedRuns: chatRunState.abortedRuns,
chatRunBuffers: chatRunState.buffers,
chatDeltaSentAt: chatRunState.deltaSentAt,
chatDeltaLastBroadcastLen: chatRunState.deltaLastBroadcastLen,
addChatRun,
removeChatRun,
subscribeSessionEvents: sessionEventSubscribers.subscribe,
unsubscribeSessionEvents: sessionEventSubscribers.unsubscribe,
subscribeSessionMessageEvents: sessionMessageSubscribers.subscribe,
unsubscribeSessionMessageEvents: sessionMessageSubscribers.unsubscribe,
unsubscribeAllSessionEvents: (connId) => {
sessionEventSubscribers.unsubscribe(connId);
sessionMessageSubscribers.unsubscribeAll(connId);
},
getSessionEventSubscriberConnIds: sessionEventSubscribers.getAll,
registerToolEventRecipient: toolEventRecipients.add,
dedupe,
wizardSessions,
findRunningWizard,
purgeWizardSession,
getRuntimeSnapshot,
startChannel,
stopChannel,
markChannelLoggedOut,
wizardRunner,
broadcastVoiceWakeChanged
};
setFallbackGatewayContextResolver(() => gatewayRequestContext);
attachGatewayWsHandlers({
wss,
clients,
port,
gatewayHost: bindHost ?? void 0,
canvasHostEnabled: Boolean(canvasHost),
canvasHostServerPort,
resolvedAuth,
rateLimiter: authRateLimiter,
browserRateLimiter: browserAuthRateLimiter,
gatewayMethods,
events: GATEWAY_EVENTS,
logGateway: log,
logHealth,
logWsControl,
extraHandlers: {
...pluginRegistry.gatewayHandlers,
...execApprovalHandlers,
...secretsHandlers
},
broadcast,
context: gatewayRequestContext
});
logGatewayStartup({
cfg: cfgAtStart,
bindHost,
bindHosts: httpBindHosts,
port,
tlsEnabled: gatewayTls.enabled,
log,
isNixMode
});
const stopGatewayUpdateCheck = minimalTestGateway ? () => {} : scheduleGatewayUpdateCheck({
cfg: cfgAtStart,
log,
isNixMode,
onUpdateAvailableChange: (updateAvailable) => {
broadcast(GATEWAY_EVENT_UPDATE_AVAILABLE, { updateAvailable }, { dropIfSlow: true });
}
});
const tailscaleCleanup = minimalTestGateway ? null : await startGatewayTailscaleExposure({
tailscaleMode,
resetOnExit: tailscaleConfig.resetOnExit,
port,
controlUiBasePath,
logTailscale
});
let browserControl = null;
if (!minimalTestGateway) {
if (deferredConfiguredChannelPluginIds.length > 0) ({pluginRegistry} = loadGatewayPlugins({
cfg: cfgAtStart,
workspaceDir: defaultWorkspaceDir,
log,
coreGatewayHandlers,
baseMethods,
logDiagnostics: false
}));
({browserControl, pluginServices} = await startGatewaySidecars({
cfg: cfgAtStart,
pluginRegistry,
defaultWorkspaceDir,
deps,
startChannels,
log,
logHooks,
logChannels,
logBrowser
}));
}
if (!minimalTestGateway) {
const hookRunner = getGlobalHookRunner();
if (hookRunner?.hasHooks("gateway_start")) hookRunner.runGatewayStart({ port }, { port }).catch((err) => {
log.warn(`gateway_start hook failed: ${String(err)}`);
});
}
const configReloader = minimalTestGateway ? { stop: async () => {} } : (() => {
const { applyHotReload, requestGatewayRestart } = createGatewayReloadHandlers({
deps,
broadcast,
getState: () => ({
hooksConfig,
hookClientIpConfig,
heartbeatRunner,
cronState,
browserControl,
channelHealthMonitor
}),
setState: (nextState) => {
hooksConfig = nextState.hooksConfig;
hookClientIpConfig = nextState.hookClientIpConfig;
heartbeatRunner = nextState.heartbeatRunner;
cronState = nextState.cronState;
cron = cronState.cron;
cronStorePath = cronState.storePath;
browserControl = nextState.browserControl;
channelHealthMonitor = nextState.channelHealthMonitor;
},
startChannel,
stopChannel,
logHooks,
logBrowser,
logChannels,
logCron,
logReload,
createHealthMonitor: (opts) => startChannelHealthMonitor({
channelManager,
checkIntervalMs: opts.checkIntervalMs,
...opts.staleEventThresholdMs != null && { staleEventThresholdMs: opts.staleEventThresholdMs },
...opts.maxRestartsPerHour != null && { maxRestartsPerHour: opts.maxRestartsPerHour }
})
});
return startGatewayConfigReloader({
initialConfig: cfgAtStart,
readSnapshot: readConfigFileSnapshot,
onHotReload: async (plan, nextConfig) => {
const previousSnapshot = getActiveSecretsRuntimeSnapshot();
const prepared = await activateRuntimeSecrets(nextConfig, {
reason: "reload",
activate: true
});
try {
await applyHotReload(plan, prepared.config);
} catch (err) {
if (previousSnapshot) activateSecretsRuntimeSnapshot(previousSnapshot);
else clearSecretsRuntimeSnapshot();
throw err;
}
},
onRestart: async (plan, nextConfig) => {
await activateRuntimeSecrets(nextConfig, {
reason: "restart-check",
activate: false
});
requestGatewayRestart(plan, nextConfig);
},
log: {
info: (msg) => logReload.info(msg),
warn: (msg) => logReload.warn(msg),
error: (msg) => logReload.error(msg)
},
watchPath: configSnapshot.path
});
})();
const close = createGatewayCloseHandler({
bonjourStop,
tailscaleCleanup,
canvasHost,
canvasHostServer,
releasePluginRouteRegistry,
stopChannel,
pluginServices,
cron,
heartbeatRunner,
updateCheckStop: stopGatewayUpdateCheck,
nodePresenceTimers,
broadcast,
tickInterval,
healthInterval,
dedupeCleanup,
mediaCleanup,
agentUnsub,
heartbeatUnsub,
transcriptUnsub,
lifecycleUnsub,
chatRunState,
clients,
configReloader,
browserControl,
wss,
httpServer,
httpServers
});
return { close: async (opts) => {
await runGlobalGatewayStopSafely({
event: { reason: opts?.reason ?? "gateway stopping" },
ctx: { port },
onError: (err) => log.warn(`gateway_stop hook failed: ${String(err)}`)
});
if (diagnosticsEnabled) stopDiagnosticHeartbeat();
if (skillsRefreshTimer) {
clearTimeout(skillsRefreshTimer);
skillsRefreshTimer = null;
}
skillsChangeUnsub();
authRateLimiter?.dispose();
browserAuthRateLimiter.dispose();
stopModelPricingRefresh();
channelHealthMonitor?.stop();
clearSecretsRuntimeSnapshot();
await close(opts);
} };
}
//#endregion
//#region src/infra/supervisor-markers.ts
const SUPERVISOR_HINTS = {
launchd: [
"LAUNCH_JOB_LABEL",
"LAUNCH_JOB_NAME",
"XPC_SERVICE_NAME",
"OPENCLAW_LAUNCHD_LABEL"
],
systemd: [
"OPENCLAW_SYSTEMD_UNIT",
"INVOCATION_ID",
"SYSTEMD_EXEC_PID",
"JOURNAL_STREAM"
],
schtasks: ["OPENCLAW_WINDOWS_TASK_NAME"]
};
[
...SUPERVISOR_HINTS.launchd,
...SUPERVISOR_HINTS.systemd,
...SUPERVISOR_HINTS.schtasks
];
function hasAnyHint(env, keys) {
return keys.some((key) => {
const value = env[key];
return typeof value === "string" && value.trim().length > 0;
});
}
function detectRespawnSupervisor(env = process.env, platform = process.platform) {
if (platform === "darwin") return hasAnyHint(env, SUPERVISOR_HINTS.launchd) ? "launchd" : null;
if (platform === "linux") return hasAnyHint(env, SUPERVISOR_HINTS.systemd) ? "systemd" : null;
if (platform === "win32") {
if (hasAnyHint(env, SUPERVISOR_HINTS.schtasks)) return "schtasks";
const marker = env.OPENCLAW_SERVICE_MARKER?.trim();
const serviceKind = env.OPENCLAW_SERVICE_KIND?.trim();
return marker && serviceKind === "gateway" ? "schtasks" : null;
}
return null;
}
//#endregion
//#region src/cli/gateway-cli/dev.ts
const DEV_IDENTITY_NAME = "C3-PO";
const DEV_IDENTITY_THEME = "protocol droid";
const DEV_IDENTITY_EMOJI = "🤖";
const DEV_AGENT_WORKSPACE_SUFFIX = "dev";
async function loadDevTemplate(name, fallback) {
try {
const templateDir = await resolveWorkspaceTemplateDir();
const raw = await fs.promises.readFile(path.join(templateDir, name), "utf-8");
if (!raw.startsWith("---")) return raw;
const endIndex = raw.indexOf("\n---", 3);
if (endIndex === -1) return raw;
return raw.slice(endIndex + 4).replace(/^\s+/, "");
} catch {
return fallback;
}
}
const resolveDevWorkspaceDir = (env = process.env) => {
const baseDir = resolveDefaultAgentWorkspaceDir(env, os.homedir);
if (env.OPENCLAW_PROFILE?.trim().toLowerCase() === "dev") return baseDir;
return `${baseDir}-${DEV_AGENT_WORKSPACE_SUFFIX}`;
};
async function writeFileIfMissing(filePath, content) {
try {
await fs.promises.writeFile(filePath, content, {
encoding: "utf-8",
flag: "wx"
});
} catch (err) {
if (err.code !== "EEXIST") throw err;
}
}
async function ensureDevWorkspace(dir) {
const resolvedDir = resolveUserPath(dir);
await fs.promises.mkdir(resolvedDir, { recursive: true });
const [agents, soul, tools, identity, user] = await Promise.all([
loadDevTemplate("AGENTS.dev.md", `# AGENTS.md - OpenClaw Dev Workspace\n\nDefault dev workspace for openclaw gateway --dev.\n`),
loadDevTemplate("SOUL.dev.md", `# SOUL.md - Dev Persona\n\nProtocol droid for debugging and operations.\n`),
loadDevTemplate("TOOLS.dev.md", `# TOOLS.md - User Tool Notes (editable)\n\nAdd your local tool notes here.\n`),
loadDevTemplate("IDENTITY.dev.md", `# IDENTITY.md - Agent Identity\n\n- Name: ${DEV_IDENTITY_NAME}\n- Creature: protocol droid\n- Vibe: ${DEV_IDENTITY_THEME}\n- Emoji: ${DEV_IDENTITY_EMOJI}\n`),
loadDevTemplate("USER.dev.md", `# USER.md - User Profile\n\n- Name:\n- Preferred address:\n- Notes:\n`)
]);
await writeFileIfMissing(path.join(resolvedDir, "AGENTS.md"), agents);
await writeFileIfMissing(path.join(resolvedDir, "SOUL.md"), soul);
await writeFileIfMissing(path.join(resolvedDir, "TOOLS.md"), tools);
await writeFileIfMissing(path.join(resolvedDir, "IDENTITY.md"), identity);
await writeFileIfMissing(path.join(resolvedDir, "USER.md"), user);
}
async function ensureDevGatewayConfig(opts) {
const workspace = resolveDevWorkspaceDir();
if (opts.reset) await handleReset("full", workspace, defaultRuntime);
const configPath = createConfigIO().configPath;
const configExists = fs.existsSync(configPath);
if (!opts.reset && configExists) return;
await writeConfigFile({
gateway: {
mode: "local",
bind: "loopback"
},
agents: {
defaults: {
workspace,
skipBootstrap: true
},
list: [{
id: "dev",
default: true,
workspace,
identity: {
name: DEV_IDENTITY_NAME,
theme: DEV_IDENTITY_THEME,
emoji: DEV_IDENTITY_EMOJI
}
}]
}
});
await ensureDevWorkspace(workspace);
defaultRuntime.log(`Dev config ready: ${shortenHomePath(configPath)}`);
defaultRuntime.log(`Dev workspace ready: ${shortenHomePath(resolveUserPath(workspace))}`);
}
//#endregion
//#region src/infra/process-respawn.ts
function isTruthy(value) {
if (!value) return false;
const normalized = value.trim().toLowerCase();
return normalized === "1" || normalized === "true" || normalized === "yes" || normalized === "on";
}
/**
* Attempt to restart this process with a fresh PID.
* - supervised environments (launchd/systemd/schtasks): caller should exit and let supervisor restart
* - OPENCLAW_NO_RESPAWN=1: caller should keep in-process restart behavior (tests/dev)
* - otherwise: spawn detached child with current argv/execArgv, then caller exits
*/
function restartGatewayProcessWithFreshPid() {
if (isTruthy(process.env.OPENCLAW_NO_RESPAWN)) return { mode: "disabled" };
const supervisor = detectRespawnSupervisor(process.env);
if (supervisor) {
if (supervisor === "launchd") {
const handoff = scheduleDetachedLaunchdRestartHandoff({
env: process.env,
mode: "start-after-exit",
waitForPid: process.pid
});
if (!handoff.ok) return {
mode: "supervised",
detail: `launchd exit fallback (${handoff.detail ?? "restart handoff failed"})`
};
return {
mode: "supervised",
detail: `launchd restart handoff pid ${handoff.pid ?? "unknown"}`
};
}
if (supervisor === "schtasks") {
const restart = triggerOpenClawRestart();
if (!restart.ok) return {
mode: "failed",
detail: restart.detail ?? `${restart.method} restart failed`
};
}
return { mode: "supervised" };
}
if (process.platform === "win32") return {
mode: "disabled",
detail: "win32: detached respawn unsupported without Scheduled Task markers"
};
try {
const args = [...process.execArgv, ...process.argv.slice(1)];
const child = spawn(process.execPath, args, {
env: process.env,
detached: true,
stdio: "inherit"
});
child.unref();
return {
mode: "spawned",
pid: child.pid ?? void 0
};
} catch (err) {
return {
mode: "failed",
detail: err instanceof Error ? err.message : String(err)
};
}
}
//#endregion
//#region src/process/restart-recovery.ts
/**
* Returns an iteration hook for in-process restart loops.
* The first call is considered initial startup and does nothing.
* Each subsequent call represents a restart iteration and invokes `onRestart`.
*/
function createRestartIterationHook(onRestart) {
let isFirstIteration = true;
return () => {
if (isFirstIteration) {
isFirstIteration = false;
return false;
}
onRestart();
return true;
};
}
//#endregion
//#region src/cli/gateway-cli/run-loop.ts
const gatewayLog$1 = createSubsystemLogger("gateway");
async function runGatewayLoop(params) {
let lock = await acquireGatewayLock({ port: params.lockPort });
let server = null;
let shuttingDown = false;
let restartResolver = null;
const cleanupSignals = () => {
process.removeListener("SIGTERM", onSigterm);
process.removeListener("SIGINT", onSigint);
process.removeListener("SIGUSR1", onSigusr1);
};
const exitProcess = (code) => {
cleanupSignals();
params.runtime.exit(code);
};
const releaseLockIfHeld = async () => {
if (!lock) return false;
await lock.release();
lock = null;
return true;
};
const reacquireLockForInProcessRestart = async () => {
try {
lock = await acquireGatewayLock({ port: params.lockPort });
return true;
} catch (err) {
gatewayLog$1.error(`failed to reacquire gateway lock for in-process restart: ${String(err)}`);
exitProcess(1);
return false;
}
};
const handleRestartAfterServerClose = async () => {
const hadLock = await releaseLockIfHeld();
const respawn = restartGatewayProcessWithFreshPid();
if (respawn.mode === "spawned" || respawn.mode === "supervised") {
const modeLabel = respawn.mode === "spawned" ? `spawned pid ${respawn.pid ?? "unknown"}` : "supervisor restart";
gatewayLog$1.info(`restart mode: full process restart (${modeLabel})`);
exitProcess(0);
return;
}
if (respawn.mode === "failed") gatewayLog$1.warn(`full process restart failed (${respawn.detail ?? "unknown error"}); falling back to in-process restart`);
else gatewayLog$1.info(`restart mode: in-process restart (${respawn.detail ?? "OPENCLAW_NO_RESPAWN"})`);
if (hadLock && !await reacquireLockForInProcessRestart()) return;
shuttingDown = false;
restartResolver?.();
};
const handleStopAfterServerClose = async () => {
await releaseLockIfHeld();
exitProcess(0);
};
const DRAIN_TIMEOUT_MS = 9e4;
const SHUTDOWN_TIMEOUT_MS = 25e3;
const request = (action, signal) => {
if (shuttingDown) {
gatewayLog$1.info(`received ${signal} during shutdown; ignoring`);
return;
}
shuttingDown = true;
const isRestart = action === "restart";
gatewayLog$1.info(`received ${signal}; ${isRestart ? "restarting" : "shutting down"}`);
const forceExitMs = isRestart ? DRAIN_TIMEOUT_MS + SHUTDOWN_TIMEOUT_MS : SHUTDOWN_TIMEOUT_MS;
const forceExitTimer = setTimeout(() => {
gatewayLog$1.error("shutdown timed out; exiting without full cleanup");
exitProcess(1);
}, forceExitMs);
(async () => {
try {
if (isRestart) {
markGatewayDraining();
const activeTasks = getActiveTaskCount();
const activeRuns = getActiveEmbeddedRunCount();
if (activeRuns > 0) abortEmbeddedPiRun(void 0, { mode: "compacting" });
if (activeTasks > 0 || activeRuns > 0) {
gatewayLog$1.info(`draining ${activeTasks} active task(s) and ${activeRuns} active embedded run(s) before restart (timeout ${DRAIN_TIMEOUT_MS}ms)`);
const [tasksDrain, runsDrain] = await Promise.all([activeTasks > 0 ? waitForActiveTasks(DRAIN_TIMEOUT_MS) : Promise.resolve({ drained: true }), activeRuns > 0 ? waitForActiveEmbeddedRuns(DRAIN_TIMEOUT_MS) : Promise.resolve({ drained: true })]);
if (tasksDrain.drained && runsDrain.drained) gatewayLog$1.info("all active work drained");
else {
gatewayLog$1.warn("drain timeout reached; proceeding with restart");
abortEmbeddedPiRun(void 0, { mode: "all" });
}
}
}
await server?.close({
reason: isRestart ? "gateway restarting" : "gateway stopping",
restartExpectedMs: isRestart ? 1500 : null
});
} catch (err) {
gatewayLog$1.error(`shutdown error: ${String(err)}`);
} finally {
clearTimeout(forceExitTimer);
server = null;
if (isRestart) await handleRestartAfterServerClose();
else await handleStopAfterServerClose();
}
})();
};
const onSigterm = () => {
gatewayLog$1.info("signal SIGTERM received");
request("stop", "SIGTERM");
};
const onSigint = () => {
gatewayLog$1.info("signal SIGINT received");
request("stop", "SIGINT");
};
const onSigusr1 = () => {
gatewayLog$1.info("signal SIGUSR1 received");
if (!consumeGatewaySigusr1RestartAuthorization()) {
if (!isGatewaySigusr1RestartExternallyAllowed()) {
gatewayLog$1.warn("SIGUSR1 restart ignored (not authorized; commands.restart=false or use gateway tool).");
return;
}
if (shuttingDown) {
gatewayLog$1.info("received SIGUSR1 during shutdown; ignoring");
return;
}
scheduleGatewaySigusr1Restart({
delayMs: 0,
reason: "SIGUSR1"
});
return;
}
markGatewaySigusr1RestartHandled();
request("restart", "SIGUSR1");
};
process.on("SIGTERM", onSigterm);
process.on("SIGINT", onSigint);
process.on("SIGUSR1", onSigusr1);
try {
const onIteration = createRestartIterationHook(() => {
resetAllLanes();
});
let isFirstStart = true;
while (true) {
onIteration();
try {
server = await params.start();
isFirstStart = false;
} catch (err) {
if (isFirstStart) throw err;
server = null;
await releaseLockIfHeld();
const errMsg = err instanceof Error ? err.message : String(err);
const errStack = err instanceof Error && err.stack ? `\n${err.stack}` : "";
gatewayLog$1.error(`gateway startup failed: ${errMsg}. Process will stay alive; fix the issue and restart.${errStack}`);
}
await new Promise((resolve) => {
restartResolver = resolve;
});
}
} finally {
await releaseLockIfHeld();
cleanupSignals();
}
}
//#endregion
//#region src/cli/gateway-cli/run.ts
const gatewayLog = createSubsystemLogger("gateway");
const GATEWAY_RUN_VALUE_KEYS = [
"port",
"bind",
"token",
"auth",
"password",
"passwordFile",
"tailscale",
"wsLog",
"rawStreamPath"
];
const GATEWAY_RUN_BOOLEAN_KEYS = [
"tailscaleResetOnExit",
"allowUnconfigured",
"dev",
"reset",
"force",
"verbose",
"claudeCliLogs",
"compact",
"rawStream"
];
const SUPERVISED_GATEWAY_LOCK_RETRY_MS = 5e3;
const GATEWAY_AUTH_MODES = [
"none",
"token",
"password",
"trusted-proxy"
];
const GATEWAY_TAILSCALE_MODES = [
"off",
"serve",
"funnel"
];
function warnInlinePasswordFlag() {
defaultRuntime.error("Warning: --password can be exposed via process listings. Prefer --password-file or OPENCLAW_GATEWAY_PASSWORD.");
}
function resolveGatewayPasswordOption(opts) {
const direct = toOptionString(opts.password);
const file = toOptionString(opts.passwordFile);
if (direct && file) throw new Error("Use either --password or --password-file.");
if (file) return readSecretFromFile(file, "Gateway password");
return direct;
}
function parseEnumOption(raw, allowed) {
if (!raw) return null;
return allowed.includes(raw) ? raw : null;
}
function formatModeChoices(modes) {
return modes.map((mode) => `"${mode}"`).join("|");
}
function formatModeErrorList(modes) {
const quoted = modes.map((mode) => `"${mode}"`);
if (quoted.length === 0) return "";
if (quoted.length === 1) return quoted[0];
if (quoted.length === 2) return `${quoted[0]} or ${quoted[1]}`;
return `${quoted.slice(0, -1).join(", ")}, or ${quoted[quoted.length - 1]}`;
}
function resolveGatewayRunOptions(opts, command) {
const resolved = { ...opts };
for (const key of GATEWAY_RUN_VALUE_KEYS) {
const inherited = inheritOptionFromParent(command, key);
if (key === "wsLog") {
resolved[key] = inherited ?? resolved[key];
continue;
}
resolved[key] = resolved[key] ?? inherited;
}
for (const key of GATEWAY_RUN_BOOLEAN_KEYS) {
const inherited = inheritOptionFromParent(command, key);
resolved[key] = Boolean(resolved[key] || inherited);
}
return resolved;
}
async function runGatewayCommand$1(opts) {
const isDevProfile = process.env.OPENCLAW_PROFILE?.trim().toLowerCase() === "dev";
const devMode = Boolean(opts.dev) || isDevProfile;
if (opts.reset && !devMode) {
defaultRuntime.error("Use --reset with --dev.");
defaultRuntime.exit(1);
return;
}
setConsoleTimestampPrefix(true);
setVerbose(Boolean(opts.verbose));
if (opts.claudeCliLogs) {
setConsoleSubsystemFilter(["agent/claude-cli"]);
process.env.OPENCLAW_CLAUDE_CLI_LOG_OUTPUT = "1";
}
const wsLogRaw = opts.compact ? "compact" : opts.wsLog;
const wsLogStyle = wsLogRaw === "compact" ? "compact" : wsLogRaw === "full" ? "full" : "auto";
if (wsLogRaw !== void 0 && wsLogRaw !== "auto" && wsLogRaw !== "compact" && wsLogRaw !== "full") {
defaultRuntime.error("Invalid --ws-log (use \"auto\", \"full\", \"compact\")");
defaultRuntime.exit(1);
}
setGatewayWsLogStyle(wsLogStyle);
if (opts.rawStream) process.env.OPENCLAW_RAW_STREAM = "1";
const rawStreamPath = toOptionString(opts.rawStreamPath);
if (rawStreamPath) process.env.OPENCLAW_RAW_STREAM_PATH = rawStreamPath;
if (devMode) await ensureDevGatewayConfig({ reset: Boolean(opts.reset) });
const cfg = loadConfig();
const portOverride = parsePort(opts.port);
if (opts.port !== void 0 && portOverride === null) {
defaultRuntime.error("Invalid port");
defaultRuntime.exit(1);
}
const port = portOverride ?? resolveGatewayPort(cfg);
if (!Number.isFinite(port) || port <= 0) {
defaultRuntime.error("Invalid port");
defaultRuntime.exit(1);
}
const bindRaw = toOptionString(opts.bind) ?? cfg.gateway?.bind ?? "loopback";
const bind = bindRaw === "loopback" || bindRaw === "lan" || bindRaw === "auto" || bindRaw === "custom" || bindRaw === "tailnet" ? bindRaw : null;
if (!bind) {
defaultRuntime.error("Invalid --bind (use \"loopback\", \"lan\", \"tailnet\", \"auto\", or \"custom\")");
defaultRuntime.exit(1);
return;
}
if (process.env.OPENCLAW_SERVICE_MARKER?.trim()) {
const stale = cleanStaleGatewayProcessesSync(port);
if (stale.length > 0) gatewayLog.info(`service-mode: cleared ${stale.length} stale gateway pid(s) before bind on port ${port}`);
}
if (opts.force) try {
const { killed, waitedMs, escalatedToSigkill } = await forceFreePortAndWait(port, {
timeoutMs: 2e3,
intervalMs: 100,
sigtermTimeoutMs: 700
});
if (killed.length === 0) gatewayLog.info(`force: no listeners on port ${port}`);
else {
for (const proc of killed) gatewayLog.info(`force: killed pid ${proc.pid}${proc.command ? ` (${proc.command})` : ""} on port ${port}`);
if (escalatedToSigkill) gatewayLog.info(`force: escalated to SIGKILL while freeing port ${port}`);
if (waitedMs > 0) gatewayLog.info(`force: waited ${waitedMs}ms for port ${port} to free`);
}
const bindWaitMs = await waitForPortBindable(port, {
timeoutMs: 3e3,
intervalMs: 150,
host: bind === "loopback" ? "127.0.0.1" : bind === "lan" ? "0.0.0.0" : bind === "custom" ? toOptionString(cfg.gateway?.customBindHost) : void 0
});
if (bindWaitMs > 0) gatewayLog.info(`force: waited ${bindWaitMs}ms for port ${port} to become bindable`);
} catch (err) {
defaultRuntime.error(`Force: ${String(err)}`);
defaultRuntime.exit(1);
return;
}
if (opts.token) {
const token = toOptionString(opts.token);
if (token) process.env.OPENCLAW_GATEWAY_TOKEN = token;
}
const authModeRaw = toOptionString(opts.auth);
const authMode = parseEnumOption(authModeRaw, GATEWAY_AUTH_MODES);
if (authModeRaw && !authMode) {
defaultRuntime.error(`Invalid --auth (use ${formatModeErrorList(GATEWAY_AUTH_MODES)})`);
defaultRuntime.exit(1);
return;
}
const tailscaleRaw = toOptionString(opts.tailscale);
const tailscaleMode = parseEnumOption(tailscaleRaw, GATEWAY_TAILSCALE_MODES);
if (tailscaleRaw && !tailscaleMode) {
defaultRuntime.error(`Invalid --tailscale (use ${formatModeErrorList(GATEWAY_TAILSCALE_MODES)})`);
defaultRuntime.exit(1);
return;
}
let passwordRaw;
try {
passwordRaw = resolveGatewayPasswordOption(opts);
} catch (err) {
defaultRuntime.error(err instanceof Error ? err.message : String(err));
defaultRuntime.exit(1);
return;
}
if (toOptionString(opts.password)) warnInlinePasswordFlag();
const tokenRaw = toOptionString(opts.token);
const snapshot = await readConfigFileSnapshot().catch(() => null);
const configExists = snapshot?.exists ?? fs.existsSync(CONFIG_PATH);
const configAuditPath = path.join(resolveStateDir(process.env), "logs", "config-audit.jsonl");
const mode = cfg.gateway?.mode;
if (!opts.allowUnconfigured && mode !== "local") {
if (!configExists) defaultRuntime.error(`Missing config. Run \`${formatCliCommand("openclaw setup")}\` or set gateway.mode=local (or pass --allow-unconfigured).`);
else {
defaultRuntime.error(`Gateway start blocked: set gateway.mode=local (current: ${mode ?? "unset"}) or pass --allow-unconfigured.`);
defaultRuntime.error(`Config write audit: ${configAuditPath}`);
}
defaultRuntime.exit(1);
return;
}
const miskeys = extractGatewayMiskeys(snapshot?.parsed);
const authOverride = authMode || passwordRaw || tokenRaw || authModeRaw ? {
...authMode ? { mode: authMode } : {},
...tokenRaw ? { token: tokenRaw } : {},
...passwordRaw ? { password: passwordRaw } : {}
} : void 0;
const resolvedAuth = resolveGatewayAuth({
authConfig: cfg.gateway?.auth,
authOverride,
env: process.env,
tailscaleMode: tailscaleMode ?? cfg.gateway?.tailscale?.mode ?? "off"
});
const resolvedAuthMode = resolvedAuth.mode;
const tokenValue = resolvedAuth.token;
const passwordValue = resolvedAuth.password;
const hasToken = typeof tokenValue === "string" && tokenValue.trim().length > 0;
const hasPassword = typeof passwordValue === "string" && passwordValue.trim().length > 0;
const tokenConfigured = hasToken || hasConfiguredSecretInput(authOverride?.token ?? cfg.gateway?.auth?.token, cfg.secrets?.defaults);
const passwordConfigured = hasPassword || hasConfiguredSecretInput(authOverride?.password ?? cfg.gateway?.auth?.password, cfg.secrets?.defaults);
const hasSharedSecret = resolvedAuthMode === "token" && tokenConfigured || resolvedAuthMode === "password" && passwordConfigured;
const canBootstrapToken = resolvedAuthMode === "token" && !tokenConfigured;
const authHints = [];
if (miskeys.hasGatewayToken) authHints.push("Found \"gateway.token\" in config. Use \"gateway.auth.token\" instead.");
if (miskeys.hasRemoteToken) authHints.push("\"gateway.remote.token\" is for remote CLI calls; it does not enable local gateway auth.");
if (resolvedAuthMode === "password" && !passwordConfigured) {
defaultRuntime.error([
"Gateway auth is set to password, but no password is configured.",
"Set gateway.auth.password (or OPENCLAW_GATEWAY_PASSWORD), or pass --password.",
...authHints
].filter(Boolean).join("\n"));
defaultRuntime.exit(1);
return;
}
if (resolvedAuthMode === "none") gatewayLog.warn("Gateway auth mode=none explicitly configured; all gateway connections are unauthenticated.");
if (bind !== "loopback" && !hasSharedSecret && !canBootstrapToken && resolvedAuthMode !== "trusted-proxy") {
defaultRuntime.error([
`Refusing to bind gateway to ${bind} without auth.`,
"Set gateway.auth.token/password (or OPENCLAW_GATEWAY_TOKEN/OPENCLAW_GATEWAY_PASSWORD) or pass --token/--password.",
...authHints
].filter(Boolean).join("\n"));
defaultRuntime.exit(1);
return;
}
const tailscaleOverride = tailscaleMode || opts.tailscaleResetOnExit ? {
...tailscaleMode ? { mode: tailscaleMode } : {},
...opts.tailscaleResetOnExit ? { resetOnExit: true } : {}
} : void 0;
const startLoop = async () => await runGatewayLoop({
runtime: defaultRuntime,
lockPort: port,
start: async () => await startGatewayServer(port, {
bind,
auth: authOverride,
tailscale: tailscaleOverride
})
});
try {
const supervisor = detectRespawnSupervisor(process.env);
while (true) try {
await startLoop();
break;
} catch (err) {
const isGatewayAlreadyRunning = err instanceof GatewayLockError && typeof err.message === "string" && err.message.includes("gateway already running");
if (!supervisor || !isGatewayAlreadyRunning) throw err;
gatewayLog.warn(`gateway already running under ${supervisor}; waiting ${SUPERVISED_GATEWAY_LOCK_RETRY_MS}ms before retrying startup`);
await new Promise((resolve) => setTimeout(resolve, SUPERVISED_GATEWAY_LOCK_RETRY_MS));
}
} catch (err) {
if (err instanceof GatewayLockError || err && typeof err === "object" && err.name === "GatewayLockError") {
const errMessage = describeUnknownError(err);
defaultRuntime.error(`Gateway failed to start: ${errMessage}\nIf the gateway is supervised, stop it with: ${formatCliCommand("openclaw gateway stop")}`);
try {
const diagnostics = await inspectPortUsage(port);
if (diagnostics.status === "busy") for (const line of formatPortDiagnostics(diagnostics)) defaultRuntime.error(line);
} catch {}
await maybeExplainGatewayServiceStop();
defaultRuntime.exit(1);
return;
}
defaultRuntime.error(`Gateway failed to start: ${String(err)}`);
defaultRuntime.exit(1);
}
}
function addGatewayRunCommand(cmd) {
return cmd.option("--port <port>", "Port for the gateway WebSocket").option("--bind <mode>", "Bind mode (\"loopback\"|\"lan\"|\"tailnet\"|\"auto\"|\"custom\"). Defaults to config gateway.bind (or loopback).").option("--token <token>", "Shared token required in connect.params.auth.token (default: OPENCLAW_GATEWAY_TOKEN env if set)").option("--auth <mode>", `Gateway auth mode (${formatModeChoices(GATEWAY_AUTH_MODES)})`).option("--password <password>", "Password for auth mode=password").option("--password-file <path>", "Read gateway password from file").option("--tailscale <mode>", `Tailscale exposure mode (${formatModeChoices(GATEWAY_TAILSCALE_MODES)})`).option("--tailscale-reset-on-exit", "Reset Tailscale serve/funnel configuration on shutdown", false).option("--allow-unconfigured", "Allow gateway start without gateway.mode=local in config", false).option("--dev", "Create a dev config + workspace if missing (no BOOTSTRAP.md)", false).option("--reset", "Reset dev config + credentials + sessions + workspace (requires --dev)", false).option("--force", "Kill any existing listener on the target port before starting", false).option("--verbose", "Verbose logging to stdout/stderr", false).option("--claude-cli-logs", "Only show claude-cli logs in the console (includes stdout/stderr)", false).option("--ws-log <style>", "WebSocket log style (\"auto\"|\"full\"|\"compact\")", "auto").option("--compact", "Alias for \"--ws-log compact\"", false).option("--raw-stream", "Log raw model stream events to jsonl", false).option("--raw-stream-path <path>", "Raw stream jsonl path").action(async (opts, command) => {
await runGatewayCommand$1(resolveGatewayRunOptions(opts, command));
});
}
//#endregion
//#region src/cli/gateway-cli/register.ts
function runGatewayCommand(action, label) {
return runCommandWithRuntime(defaultRuntime, action, (err) => {
const message = String(err);
defaultRuntime.error(label ? `${label}: ${message}` : message);
defaultRuntime.exit(1);
});
}
function parseDaysOption(raw, fallback = 30) {
if (typeof raw === "number" && Number.isFinite(raw)) return Math.max(1, Math.floor(raw));
if (typeof raw === "string" && raw.trim() !== "") {
const parsed = Number(raw);
if (Number.isFinite(parsed)) return Math.max(1, Math.floor(parsed));
}
return fallback;
}
function resolveGatewayRpcOptions(opts, command) {
const parentToken = inheritOptionFromParent(command, "token");
const parentPassword = inheritOptionFromParent(command, "password");
return {
...opts,
token: opts.token ?? parentToken,
password: opts.password ?? parentPassword
};
}
function renderCostUsageSummary(summary, days, rich) {
const totalCost = formatUsd(summary.totals.totalCost) ?? "$0.00";
const totalTokens = formatTokenCount(summary.totals.totalTokens) ?? "0";
const lines = [colorize(rich, theme.heading, `Usage cost (${days} days)`), `${colorize(rich, theme.muted, "Total:")} ${totalCost} · ${totalTokens} tokens`];
if (summary.totals.missingCostEntries > 0) lines.push(`${colorize(rich, theme.muted, "Missing entries:")} ${summary.totals.missingCostEntries}`);
const latest = summary.daily.at(-1);
if (latest) {
const latestCost = formatUsd(latest.totalCost) ?? "$0.00";
const latestTokens = formatTokenCount(latest.totalTokens) ?? "0";
lines.push(`${colorize(rich, theme.muted, "Latest day:")} ${latest.date} · ${latestCost} · ${latestTokens} tokens`);
}
return lines;
}
function registerGatewayCli(program) {
const gateway = addGatewayRunCommand(program.command("gateway").description("Run, inspect, and query the WebSocket Gateway").addHelpText("after", () => `\n${theme.heading("Examples:")}\n${formatHelpExamples([
["openclaw gateway run", "Run the gateway in the foreground."],
["openclaw gateway status", "Show service status and probe reachability."],
["openclaw gateway discover", "Find local and wide-area gateway beacons."],
["openclaw gateway call health", "Call a gateway RPC method directly."]
])}\n\n${theme.muted("Docs:")} ${formatDocsLink("/cli/gateway", "docs.openclaw.ai/cli/gateway")}\n`));
addGatewayRunCommand(gateway.command("run").description("Run the WebSocket Gateway (foreground)"));
addGatewayServiceCommands(gateway, { statusDescription: "Show gateway service status + probe the Gateway" });
gatewayCallOpts(gateway.command("call").description("Call a Gateway method").argument("<method>", "Method name (health/status/system-presence/cron.*)").option("--params <json>", "JSON object string for params", "{}").action(async (method, opts, command) => {
await runGatewayCommand(async () => {
const rpcOpts = resolveGatewayRpcOptions(opts, command);
const config = await readBestEffortConfig();
const params = JSON.parse(String(opts.params ?? "{}"));
const result = await callGatewayCli(method, {
...rpcOpts,
config
}, params);
if (rpcOpts.json) {
defaultRuntime.writeJson(result);
return;
}
const rich = isRich();
defaultRuntime.log(`${colorize(rich, theme.heading, "Gateway call")}: ${colorize(rich, theme.muted, String(method))}`);
defaultRuntime.writeJson(result);
}, "Gateway call failed");
}));
gatewayCallOpts(gateway.command("usage-cost").description("Fetch usage cost summary from session logs").option("--days <days>", "Number of days to include", "30").action(async (opts, command) => {
await runGatewayCommand(async () => {
const rpcOpts = resolveGatewayRpcOptions(opts, command);
const days = parseDaysOption(opts.days);
const config = await readBestEffortConfig();
const result = await callGatewayCli("usage.cost", {
...rpcOpts,
config
}, { days });
if (rpcOpts.json) {
defaultRuntime.writeJson(result);
return;
}
const rich = isRich();
const summary = result;
for (const line of renderCostUsageSummary(summary, days, rich)) defaultRuntime.log(line);
}, "Gateway usage cost failed");
}));
gatewayCallOpts(gateway.command("health").description("Fetch Gateway health").action(async (opts, command) => {
await runGatewayCommand(async () => {
const rpcOpts = resolveGatewayRpcOptions(opts, command);
const config = await readBestEffortConfig();
const result = await callGatewayCli("health", {
...rpcOpts,
config
});
if (rpcOpts.json) {
defaultRuntime.writeJson(result);
return;
}
const rich = isRich();
const obj = result && typeof result === "object" ? result : {};
const durationMs = typeof obj.durationMs === "number" ? obj.durationMs : null;
defaultRuntime.log(colorize(rich, theme.heading, "Gateway Health"));
defaultRuntime.log(`${colorize(rich, theme.success, "OK")}${durationMs != null ? ` (${durationMs}ms)` : ""}`);
if (obj.channels && typeof obj.channels === "object") for (const line of formatHealthChannelLines(obj)) defaultRuntime.log(styleHealthChannelLine(line, rich));
});
}));
gateway.command("probe").description("Show gateway reachability + discovery + health + status summary (local + remote)").option("--url <url>", "Explicit Gateway WebSocket URL (still probes localhost)").option("--ssh <target>", "SSH target for remote gateway tunnel (user@host or user@host:port)").option("--ssh-identity <path>", "SSH identity file path").option("--ssh-auto", "Try to derive an SSH target from Bonjour discovery", false).option("--token <token>", "Gateway token (applies to all probes)").option("--password <password>", "Gateway password (applies to all probes)").option("--timeout <ms>", "Overall probe budget in ms", "3000").option("--json", "Output JSON", false).action(async (opts, command) => {
await runGatewayCommand(async () => {
await gatewayStatusCommand(resolveGatewayRpcOptions(opts, command), defaultRuntime);
});
});
gateway.command("discover").description("Discover gateways via Bonjour (local + wide-area if configured)").option("--timeout <ms>", "Per-command timeout in ms", "2000").option("--json", "Output JSON", false).action(async (opts) => {
await runGatewayCommand(async () => {
const wideAreaDomain = resolveWideAreaDiscoveryDomain({ configDomain: (await readBestEffortConfig()).discovery?.wideArea?.domain });
const timeoutMs = parseDiscoverTimeoutMs(opts.timeout, 2e3);
const domains = ["local.", ...wideAreaDomain ? [wideAreaDomain] : []];
const deduped = dedupeBeacons(await withProgress({
label: "Scanning for gateways…",
indeterminate: true,
enabled: opts.json !== true,
delayMs: 0
}, async () => await discoverGatewayBeacons({
timeoutMs,
wideAreaDomain
}))).toSorted((a, b) => String(a.displayName || a.instanceName).localeCompare(String(b.displayName || b.instanceName)));
if (opts.json) {
const enriched = deduped.map((b) => {
const host = pickBeaconHost(b);
const port = pickGatewayPort(b);
return {
...b,
wsUrl: host ? `ws://${host}:${port}` : null
};
});
defaultRuntime.writeJson({
timeoutMs,
domains,
count: enriched.length,
beacons: enriched
});
return;
}
const rich = isRich();
defaultRuntime.log(colorize(rich, theme.heading, "Gateway Discovery"));
defaultRuntime.log(colorize(rich, theme.muted, `Found ${deduped.length} gateway(s) · domains: ${domains.join(", ")}`));
if (deduped.length === 0) return;
for (const beacon of deduped) for (const line of renderBeaconLines(beacon, rich)) defaultRuntime.log(line);
}, "gateway discover failed");
});
}
//#endregion
export { registerGatewayCli };