openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
166 lines (165 loc) • 9.26 kB
JavaScript
import { f as resolveAgentIdFromSessionKey } from "./session-key-BnWWjqNc.js";
import { t as ADMIN_SCOPE } from "./operator-scopes-Dw7Gu2cA.js";
import { r as GATEWAY_CLIENT_MODES } from "./client-info-B1bPgeKr.js";
import { t as ErrorCodes } from "./gateway-error-details-w0nAGBBp.js";
import { t as setSafeTimeout } from "./timer-delay-x5n129Nx.js";
import "./method-scopes-K6J_UQGL.js";
import { d as errorShape } from "./error-codes-Bo8q2D1o.js";
import { l as resolveAgentHarnessSessionContextError, t as AGENT_HARNESS_MODEL_RUN_FORBIDDEN_MESSAGE, u as resolveAgentHarnessSessionIdMismatchError } from "./agent-harness-session-key-BOz3yx0-.js";
import { n as getCliSessionBinding } from "./cli-session-binding-Aa6nlDa8.js";
import { s as resolveCliRuntimeExecutionProvider } from "./model-runtime-aliases-BJ2qM8I_.js";
import { s as isCliProvider } from "./model-selection-di2kjKCB.js";
import "./sessions-9nxpeTwt.js";
import { f as resolveSessionWorkStartError } from "./lifecycle-BaroCBMc.js";
import "./cli-session-pAa_-y_N.js";
import "./subagent-requester-store-key-H6AgjPG7.js";
import { l as resolveDeletedAgentIdFromSessionKey, r as loadGatewaySessionEntry } from "./session-utils-store-CInT2loy.js";
import "./session-utils-Cai0_C6U.js";
import { a as emitGatewaySessionStartPluginHook, i as emitGatewaySessionEndPluginHook } from "./session-reset-service-DsSXKjai.js";
//#region src/gateway/agent-turn/agent-handler-helpers.ts
const CRON_CONTINUATION_RELEASE_RECOVERY_DELAYS_MS = [
250,
1e3,
4e3,
15e3
];
function clientHasAdminScope(client) {
return (Array.isArray(client?.connect?.scopes) ? client.connect.scopes : []).includes(ADMIN_SCOPE);
}
function respondDeletedAgentSession(params) {
const deletedAgentId = resolveDeletedAgentIdFromSessionKey(params.cfg, params.canonicalKey, params.entry, { acpMetadataSessionKey: params.acpMetadataSessionKey ?? params.canonicalKey });
if (deletedAgentId === null) return false;
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `Agent "${deletedAgentId}" no longer exists in configuration`));
return true;
}
function respondUnavailableAgentSessionForKey(params) {
const { cfg, entry, canonicalKey, legacyKey } = loadGatewaySessionEntry(params.sessionKey, {
...params.agentId ? { agentId: params.agentId } : {},
clone: false,
projection: "list"
});
if (respondDeletedAgentSession({
cfg,
canonicalKey,
entry,
acpMetadataSessionKey: legacyKey,
respond: params.respond
})) return true;
const harnessSessionError = resolveAgentHarnessSessionContextError(canonicalKey, entry);
if (harnessSessionError) {
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, harnessSessionError));
return true;
}
const harnessSessionIdError = resolveAgentHarnessSessionIdMismatchError(entry, params.requestedSessionId);
if (harnessSessionIdError) {
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, harnessSessionIdError));
return true;
}
if (params.isRawModelRun && entry?.modelSelectionLocked === true) {
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, AGENT_HARNESS_MODEL_RUN_FORBIDDEN_MESSAGE));
return true;
}
const archivedSessionError = resolveSessionWorkStartError(canonicalKey, entry);
if (!archivedSessionError) return false;
params.respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, archivedSessionError));
return true;
}
function resolveAllowModelOverrideFromClient(client) {
return clientHasAdminScope(client) || client?.internal?.allowModelOverride === true;
}
function resolveCanUseInternalRuntimeHandoff(client) {
return client?.connect?.client?.mode === GATEWAY_CLIENT_MODES.BACKEND;
}
function resolveCanUseCronRunContinuation(client) {
return client?.internal?.cronRunContinuation === true;
}
function cronContinuationHasReusableRuntime(params) {
const executionProvider = resolveCliRuntimeExecutionProvider({
provider: params.provider,
cfg: params.cfg,
agentId: params.agentId,
modelId: params.model
}) ?? params.provider;
return !isCliProvider(executionProvider, params.cfg) || Boolean(getCliSessionBinding(params.entry, executionProvider)?.sessionId);
}
function withoutCronRunContinuation(entry) {
const { cronRunContinuation: _cronRunContinuation, ...baseEntry } = entry;
return baseEntry;
}
function emitAgentSendSessionLifecycleTransition(transition) {
if (!transition) return;
if (transition.previousSessionId) emitGatewaySessionEndPluginHook({
cfg: transition.cfg,
sessionKey: transition.sessionKey,
sessionId: transition.previousSessionId,
storePath: transition.storePath,
sessionFile: transition.previousSessionFile,
agentId: transition.agentId,
workspaceDir: transition.workspaceDir,
reason: transition.previousEndReason ?? "unknown",
nextSessionId: transition.sessionId,
nextSessionKey: transition.sessionKey
});
emitGatewaySessionStartPluginHook({
cfg: transition.cfg,
sessionKey: transition.sessionKey,
sessionId: transition.sessionId,
resumedFrom: transition.previousSessionId,
storePath: transition.storePath,
sessionFile: transition.sessionFile,
agentId: transition.agentId
});
}
function shouldSuppressAgentPromptPersistence(params) {
return params.inputProvenance?.kind === "inter_session" && params.inputProvenance.sourceTool === "subagent_announce" && params.internalEvents?.some((event) => event.type === "task_completion" && event.source === "subagent") === true;
}
function withSqliteSessionFileMarker(params) {
if (!(params.agentId ?? resolveAgentIdFromSessionKey(params.sessionKey))) return params.entry;
return params.entry;
}
function yieldAfterAgentAcceptedAck() {
return new Promise((resolve) => {
setTimeout(resolve, 10);
});
}
function waitForCronContinuationReleaseRecovery(delayMs) {
return new Promise((resolve) => {
setSafeTimeout(resolve, delayMs).unref?.();
});
}
//#endregion
//#region src/gateway/server-methods/cron-creator-authority-admission.ts
function resolveDirectOperatorAuthority(params) {
const internal = params.client?.internal;
const runId = params.runId.trim();
return runId.length > 0 && clientHasAdminScope(params.client ?? null) && (internal?.isLocalClient === true || internal?.controlUiAdmin === true) && Boolean(params.resolvedSessionKey?.trim()) && !params.spawnedBy?.trim() && params.inputProvenance === void 0 && !params.disallowed && internal.syntheticClient !== true && internal.senderAttribution === void 0 && internal.approvalRuntime !== true && internal.cronRunContinuation !== true && internal.agentRuntimeIdentity === void 0 && internal.pluginRuntimeOwnerId === void 0 && internal.agentRunTracking === void 0 && internal.pluginSubagentRequester === void 0 && internal.runtimePluginToolGrant === void 0 && internal.delegatedToolPolicyHandoffId === void 0 ? Object.freeze({
runId,
callerOrigin: internal?.isLocalClient === true ? { kind: "local" } : { kind: "unknown" },
...internal?.controlUiAdmin === true ? { controlUiAdmin: true } : {}
}) : void 0;
}
/** Mints cron authority for an admitted local operator or authenticated Control UI admin turn. */
function resolveGatewayCronCreatorAuthorityAdmission(params) {
const request = params.request;
return resolveDirectOperatorAuthority({
runId: params.runId,
resolvedSessionKey: params.resolvedSessionKey,
spawnedBy: params.spawnedBy,
client: params.client,
inputProvenance: params.inputProvenance,
disallowed: params.hasRestoredCronContinuation || params.isOneShotModelRun || params.isRestartRecoveryResumeRun || request.modelRun === true || request.acpTurnSource !== void 0 || request.internalRuntimeHandoffId !== void 0 || request.internalExecutionIdentityRetry === true || request.internalExecutionIdentityRecoveryAttempt !== void 0 || request.execApprovalFollowupExpectedSessionId !== void 0 || request.internalEvents !== void 0 || request.sessionEffects === "internal" || request.suppressPromptPersistence === true || request.swarmCollector === true || request.lane === "subagent"
});
}
/** Mints the same authority for an admitted ordinary operator chat.send turn. */
function resolveGatewayChatCronCreatorAuthorityAdmission(params) {
return resolveDirectOperatorAuthority({
runId: params.runId,
resolvedSessionKey: params.resolvedSessionKey,
spawnedBy: params.spawnedBy,
client: params.client,
inputProvenance: params.inputProvenance,
disallowed: !params.isDirectExternalUser || params.hasExplicitOrigin || params.hasRestoredCronContinuation || params.isIncognito || params.isReconnectResume || params.isSystemGenerated || params.turnKind !== "main"
});
}
//#endregion
export { cronContinuationHasReusableRuntime as a, resolveCanUseCronRunContinuation as c, respondUnavailableAgentSessionForKey as d, shouldSuppressAgentPromptPersistence as f, yieldAfterAgentAcceptedAck as g, withoutCronRunContinuation as h, clientHasAdminScope as i, resolveCanUseInternalRuntimeHandoff as l, withSqliteSessionFileMarker as m, resolveGatewayCronCreatorAuthorityAdmission as n, emitAgentSendSessionLifecycleTransition as o, waitForCronContinuationReleaseRecovery as p, CRON_CONTINUATION_RELEASE_RECOVERY_DELAYS_MS as r, resolveAllowModelOverrideFromClient as s, resolveGatewayChatCronCreatorAuthorityAdmission as t, respondDeletedAgentSession as u };