UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

385 lines (384 loc) • 15.7 kB
import { c as normalizeOptionalLowercaseString, l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs"; import { c as normalizeOptionalTrimmedStringList } from "./string-normalization-DLmfYZXI.mjs"; import { r as normalizeProviderId } from "./provider-id-b5XGSf9C.mjs"; import { i as normalizeChatChannelId } from "./ids-BRSWrLzE.mjs"; import { T as selectLiveRuntimeConfig } from "./runtime-snapshot-WbwbY5b7.mjs"; import { i as resolveProviderIdForAuth } from "./provider-auth-aliases-CgGlzANu.mjs"; import { C as findModelInCatalog, T as prepareModelRunCapabilities } from "./model-selection-shared-9Kz63Hhm.mjs"; import { t as normalizeChatType } from "./chat-type-CL5nBDaO.mjs"; import { a as readUserTurnPromptReactionSource } from "./user-turn-transcript-admission-BdVewOwR.mjs"; import { t as normalizeAnyChannelId } from "./registry-normalize-guW5_4yo.mjs"; import "./registry-BB86xKJa.mjs"; import { c as resolveMessageActionTurnCapabilityLifetime, n as isTrustedMessageActionTurnIngress, r as mintMessageActionTurnCapability } from "./message-action-turn-capability-CQk0hiot.mjs"; import { t as getChannelPlugin } from "./registry-ByqVE5S3.mjs"; import { a as isInternalMessageChannel } from "./message-channel-DTYVLJT1.mjs"; import { a as readChannelSourceTurnId } from "./session-event-handoff-CC7Ui3SA.mjs"; import "./plugins-BoMiRtiY.mjs"; import "./config-CMA-J084.mjs"; import { i as resolveOriginMessageProvider } from "./reply-tool-authority-inbound-BQ8cxNJQ.mjs"; import { a as resolveCandidateThinkingLevel, r as normalizeThinkingCatalogProviders } from "./thinking-runtime-BKWl4CUs.mjs"; import { t as resolveFastModeState } from "./fast-mode-BcwTQ_IZ.mjs"; import { t as hasInboundAudio } from "./inbound-media-BJ8l1Cri.mjs"; import { t as resolveCommandSecretRefsViaGateway } from "./command-secret-gateway-Dh_PhH7v.mjs"; import { n as getAgentRuntimeOptionalCommandSecretPaths, t as getAgentRuntimeCommandSecretTargetIds, u as getScopedChannelsCommandSecretTargets } from "./command-secret-targets-CH2XIeV_.mjs"; import { t as isReasoningTagProvider } from "./provider-utils-D545sPXZ.mjs"; import { n as buildAgentHookContextIdentityFields, t as buildAgentHookContextChannelFields } from "./hook-agent-context-sUFGgK45.mjs"; import { a as resolveReplyScheduledToolPolicy, i as resolveModelFallbackOptions, n as buildReplyRunStateParams, o as resolveRunModelHasVision } from "./agent-runner-run-params-CiEn1JED.mjs"; import { t as resolveMessageSecretScope } from "./message-secret-scope-Cz7XDJ3I.mjs"; //#region src/auto-reply/reply/agent-runner-auth-profile.ts /** Keeps an auth profile only when the current provider shares the primary auth scope. */ function resolveRunAuthProfile(e, t, n) { const { provider: r, authProfileId: i, authProfileIdSource: a } = e; const o = { config: n?.config ?? e.config, workspaceDir: e.workspaceDir }; const s = normalizeProviderId(t); const c = normalizeProviderId(r); const l = s !== "" && s === c || resolveProviderIdForAuth(t, o) === resolveProviderIdForAuth(r, o) ? i : void 0; return { authProfileId: l, authProfileIdSource: l ? a : void 0 }; } /** Applies an auto-fallback probe's pinned auth to its fallback candidate. */ function resolveFallbackCandidateRun(e, t, n) { const r = e.autoFallbackPrimaryProbe; const i = r && t === r.provider && n === r.model; if (!r || t !== r.fallbackProvider || i || !r.fallbackAuthProfileId) return e; const a = { ...e, provider: t, model: n, authProfileId: r.fallbackAuthProfileId }; if (r.fallbackAuthProfileIdSource) a.authProfileIdSource = r.fallbackAuthProfileIdSource; else delete a.authProfileIdSource; return a; } //#endregion //#region src/auto-reply/reply/agent-runner-utils.ts async function resolveQueuedReplyExecutionConfig(e, t) { const n = selectLiveRuntimeConfig(e); const { resolvedConfig: r } = await resolveCommandSecretRefsViaGateway({ config: n, commandName: "reply", targetIds: getAgentRuntimeCommandSecretTargetIds({ config: n }), optionalActivePaths: getAgentRuntimeOptionalCommandSecretPaths(n) }); const i = r ?? n; const a = resolveMessageSecretScope({ channel: t?.originatingChannel, fallbackChannel: t?.messageProvider, accountId: t?.originatingAccountId, fallbackAccountId: t?.agentAccountId }); if (!a.channel) return i; const o = getScopedChannelsCommandSecretTargets({ config: i, channel: a.channel, accountId: a.accountId }); if (o.targetIds.size === 0) return i; return (await resolveCommandSecretRefsViaGateway({ config: i, commandName: "reply", targetIds: o.targetIds, ...o.allowedPaths ? { allowedPaths: o.allowedPaths } : {} })).resolvedConfig ?? i; } function buildThreadingToolContext(e) { const { sessionCtx: t, config: n, hasRepliedRef: r } = e; const i = t.InputProvenance?.kind === "internal_system" && t.InputProvenance.sourceTool === "restart-sentinel" ? t.ReplyToId : isInternalMessageChannel(t.Provider ?? t.Surface) ? void 0 : t.MessageSidFull ?? t.MessageSid; const a = readChannelSourceTurnId(t); const o = resolveOriginMessageProvider({ originatingChannel: t.OriginatingChannel, provider: t.Provider }); const s = t.OriginatingTo ?? t.To; const c = n ? normalizeOptionalLowercaseString(o) : void 0; if (!n || !c) return { currentMessageId: i, currentSourceTurnId: a, replyToMode: t.ReplyToMode }; const l = normalizeChatChannelId(c) ?? normalizeAnyChannelId(c); const u = l ? getChannelPlugin(l)?.threading : void 0; const d = u?.buildToolContext ? u.buildToolContext({ cfg: n, accountId: t.AccountId, context: { Channel: o, From: t.From, To: s, ChatType: t.ChatType, CurrentMessageId: i, ReplyToMode: t.ReplyToMode, ReplyToId: t.ReplyToId, ReplyToIdFull: t.ReplyToIdFull, ThreadLabel: t.ThreadLabel, MessageThreadId: t.MessageThreadId, TransportThreadId: t.TransportThreadId, NativeChannelId: t.NativeChannelId }, hasRepliedRef: r }) ?? {} : { currentChannelId: normalizeOptionalString(s), hasRepliedRef: r }; const f = Object.hasOwn(d, "currentMessageId"); return { ...d, currentChannelProvider: l ?? c, currentMessageId: f ? d.currentMessageId : i, currentSourceTurnId: a, replyToMode: d.replyToMode ?? t.ReplyToMode }; } function resolveFollowupCurrentMessageId(e) { return e.run.inputProvenance?.kind === "internal_system" && e.run.inputProvenance.sourceTool === "restart-sentinel" ? e.originatingReplyToId : e.messageId; } /** Remaps the original inline request without reusing a queued model's clamped level. */ function resolveRunThinkingLevelForFallbackCandidate(e) { const { run: t, ...n } = e; return resolveCandidateThinkingLevel({ ...n, level: t.thinkLevelOverride === "default" ? t.thinkLevel : t.thinkLevelOverride ?? t.thinkLevel }); } function resolveRunFastModeForFallbackCandidate(e) { const t = resolveFastModeState({ cfg: e.config, provider: e.provider, model: e.model, agentId: e.run.agentId, sessionEntry: e.sessionEntry }); return { fastMode: e.run.fastModeOverride ? e.run.fastMode : t.requestMode, fastModeAutoOnSeconds: e.run.fastModeAutoOnSecondsOverride ? e.run.fastModeAutoOnSeconds : t.fastAutoOnSeconds }; } function applyReplyRouteToTemplate(e) { return { ...e.sessionCtx, OriginatingChannel: e.replyRoute?.originatingChannel ?? e.sessionCtx.OriginatingChannel, OriginatingTo: e.replyRoute?.originatingTo ?? e.sessionCtx.OriginatingTo, AccountId: e.replyRoute?.originatingAccountId ?? e.sessionCtx.AccountId ?? e.run.agentAccountId, ChatType: normalizeChatType(e.replyRoute?.originatingChatType) ?? normalizeChatType(e.sessionCtx.ChatType) ?? e.run.chatType, MessageThreadId: e.replyRoute?.originatingThreadId ?? e.sessionCtx.MessageThreadId, ReplyToId: e.replyRoute?.originatingReplyToId ?? e.sessionCtx.ReplyToId, ReplyToMode: e.replyRoute?.originatingReplyToMode ?? e.sessionCtx.ReplyToMode }; } /** * Builds message-tool threading context for the queued reply route. Embedded * and CLI runtimes share it so the channel adapter decides the default thread. */ function buildReplyRouteThreadingToolContext(e) { return buildThreadingToolContext({ sessionCtx: applyReplyRouteToTemplate(e), config: e.run.config, hasRepliedRef: e.hasRepliedRef }); } function buildEmbeddedContextFromTemplate(e) { const t = applyReplyRouteToTemplate(e); return { sessionId: e.run.sessionId, sessionKey: e.run.sessionKey, sandboxSessionKey: e.run.runtimePolicySessionKey, agentId: e.run.agentId, messageProvider: resolveOriginMessageProvider({ originatingChannel: t.OriginatingChannel, provider: t.Provider }), ...t.ChatType ? { chatType: t.ChatType } : {}, agentAccountId: t.AccountId, conversationRoutePeerId: e.run.conversationRoutePeerId, messageTo: t.OriginatingTo ?? t.To, messageThreadId: t.MessageThreadId, chatId: normalizeOptionalString(t.NativeChannelId) ?? normalizeOptionalString(t.ChatId), memberRoleIds: normalizeOptionalTrimmedStringList(t.MemberRoleIds), ...buildThreadingToolContext({ sessionCtx: t, config: e.run.config, hasRepliedRef: e.hasRepliedRef }), currentInboundAudio: hasInboundAudio(t) }; } function buildTemplateSenderContext(e) { return { senderId: normalizeOptionalString(e.SenderId), channelContext: e.ChannelContext, senderName: normalizeOptionalString(e.SenderName), senderUsername: normalizeOptionalString(e.SenderUsername), senderE164: normalizeOptionalString(e.SenderE164) }; } /** Reuse the candidate's channel and sender projection for early model routing. */ function buildModelResolveContext(e) { const t = { ...buildEmbeddedContextFromTemplate(e), ...buildTemplateSenderContext(e.sessionCtx), ...buildReplyRunStateParams(e.run), trigger: e.trigger }; return { trigger: t.trigger, ...buildAgentHookContextChannelFields(t), ...buildAgentHookContextIdentityFields(t) }; } /** Bind either runtime to the same trusted source turn and requester. */ function mintReplyMessageActionTurnCapability(e, t) { if (e.followupRun.run.internalEventExecution || e.followupRun.run.sessionEventOrigin) return; const n = isTrustedMessageActionTurnIngress(e.sessionCtx.Provider); const r = e.opts?.dashboardReadAdmission; if (!n && e.sessionCtx.Provider !== "webchat") return; const i = buildEmbeddedContextFromTemplate({ run: e.followupRun.run, replyRoute: e.followupRun, sessionCtx: e.sessionCtx, hasRepliedRef: e.opts?.hasRepliedRef }); const a = e.runtimePolicySessionKey ?? i.sessionKey; if (!i.agentId || !a) return; if (!n) { const n = r?.runId === t && r.agentId === i.agentId && r.sessionKey === a && r.sessionId === i.sessionId ? r : void 0; const o = e.followupRun.userTurnTranscriptRecorder; const s = readUserTurnPromptReactionSource(o); const c = s && s.agentId === i.agentId && s.sessionKey === a ? s : void 0; if (!n && !c) return; n?.assertCurrent(); c?.assertCurrent(); return mintMessageActionTurnCapability({ agentId: i.agentId, runId: t, sessionKey: a, sessionId: i.sessionId, assertDashboardReadCurrent: n?.assertCurrent, promptReactionSource: c && o ? { source: c, recorder: o } : void 0, expiresWithRun: true }); } if (!i.messageProvider || !i.currentChannelId) return; const o = buildTemplateSenderContext(e.sessionCtx); return mintMessageActionTurnCapability({ agentId: i.agentId, runId: t, sessionKey: a, sourceReplySessionKey: i.sessionKey, sessionId: i.sessionId, requesterAccountId: i.agentAccountId, requesterSenderId: o.senderId, requesterSenderName: o.senderName, requesterSenderUsername: o.senderUsername, requesterSenderE164: o.senderE164, toolContext: { currentChannelId: i.currentChannelId, currentChatType: i.chatType, currentMessagingTarget: i.currentMessagingTarget, currentGraphChannelId: i.currentGraphChannelId, currentChannelProvider: i.currentChannelProvider, currentThreadTs: i.currentThreadTs, currentMessageId: i.currentMessageId, currentSourceTurnId: i.currentSourceTurnId, replyToMode: i.replyToMode, hasRepliedRef: i.hasRepliedRef, sameChannelThreadRequired: i.sameChannelThreadRequired }, ...resolveMessageActionTurnCapabilityLifetime(e.followupRun.run.timeoutMs) }); } async function buildEmbeddedRunExecutionParams(e) { const t = resolveRunAuthProfile(e.run, e.provider); const n = buildEmbeddedContextFromTemplate(e); const r = buildTemplateSenderContext(e.sessionCtx); const i = e.run.config; const a = e.run.scheduledAutomation; a?.assertCurrent(); const o = a?.job; const s = resolveReplyScheduledToolPolicy(e.run); const { modelFallbackAvailability: c, fallbacksOverride: l } = resolveModelFallbackOptions(e.run); let u; if (e.agentRuntime) { const { loadProviderScopedThinkingCatalog: t } = await import("./agents/model-catalog.runtime.js"); const n = normalizeThinkingCatalogProviders(await t({ config: i, provider: e.provider, model: e.model, agentRuntime: e.agentRuntime, agentId: e.run.agentId, agentDir: e.run.agentDir, workspaceDir: e.run.workspaceDir })); const r = findModelInCatalog(n, e.provider, e.model) ? n : e.run.thinkingCatalog; u = prepareModelRunCapabilities([r, []], [ e.provider, e.model, e.agentRuntime ]).modelThinkingCapability; } const d = !e.run.skipProviderRuntimeHints && (e.run.enforceFinalTag || isReasoningTagProvider(e.provider, { config: i, workspaceDir: e.run.workspaceDir, modelId: e.model })); const f = await resolveRunModelHasVision(e); a?.assertCurrent(); return { ...n, ...r, ...buildReplyRunStateParams(e.run), providerReviewAcknowledgment: e.run.providerReviewAcknowledgment, permissionMode: e.run.permissionMode, sessionRoot: e.run.sessionRoot, blindPrompt: e.run.blindPrompt, agentDir: e.run.agentDir, config: i, trustedInternalHandoff: e.run.trustedInternalHandoff, scheduledToolPolicy: s, ...o ? { jobId: o.id, scheduledRuntimeAuthority: o.runtimeAuthority, scheduledRuntimeAuthorityRecoveryRequired: o.runtimeAuthorityRecoveryRequired === true } : {}, runtimePluginToolGrant: e.run.runtimePluginToolGrant, enforceFinalTag: d, silentExpected: e.run.silentExpected, silentReplyPromptMode: e.run.silentReplyPromptMode, sourceReplyDeliveryMode: e.run.sourceReplyDeliveryMode, toolBindings: e.run.toolBindings, skillLibraryAuthoring: e.run.skillLibraryAuthoring, provider: e.provider, model: e.model, modelHasVision: f, ...u ? { modelThinkingCapability: u } : {}, requestedRouteResolution: "resolved", modelSelectionLocked: e.run.modelSelectionLocked, modelFallbackAvailability: c, modelFallbacksOverride: l, ...t, thinkLevel: e.run.thinkLevel, thinkingExplicit: e.run.thinkingExplicit, cyberAccessProgram: e.run.cyberAccessProgram, fastMode: e.run.fastMode, fastModeAutoOnSeconds: e.run.fastModeAutoOnSeconds, verboseLevel: e.run.verboseLevel, reasoningLevel: e.run.reasoningLevel, execOverrides: e.run.execOverrides, bashElevated: e.run.bashElevated, timeoutMs: e.run.timeoutMs, runTimeoutOverrideMs: e.run.runTimeoutOverrideMs, runId: e.runId, promptCacheKey: e.promptCacheKey, allowTransientCooldownProbe: e.allowTransientCooldownProbe }; } //#endregion export { mintReplyMessageActionTurnCapability as a, resolveRunFastModeForFallbackCandidate as c, resolveRunAuthProfile as d, buildThreadingToolContext as i, resolveRunThinkingLevelForFallbackCandidate as l, buildModelResolveContext as n, resolveFollowupCurrentMessageId as o, buildReplyRouteThreadingToolContext as r, resolveQueuedReplyExecutionConfig as s, buildEmbeddedRunExecutionParams as t, resolveFallbackCandidateRun as u };