openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
98 lines (97 loc) • 5.17 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { n as isArtifactPreservingStateRead } from "./artifact-preserving-state-reads-BfhmBr-V.mjs";
import { t as INTERNAL_MESSAGE_CHANNEL } from "./message-channel-constants-CkbqRgGl.mjs";
import { s as isOperatorUiClient } from "./message-channel-DTYVLJT1.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { f as resolveGatewayOperatorRoleActor } from "./operator-role-policy-DhcqVpmG.mjs";
import { t as ConnectErrorDetailCodes } from "./connect-error-details-BdhXf496.mjs";
import { n as withCommandSenderAuthority, t as getCommandSenderAuthority } from "./command-sender-authority-DS9M5Fsl.mjs";
import { n as gitHubPublicApi } from "./github-public-api-C47tI7vQ.mjs";
import { r as isSyntheticGatewayCaller } from "./gateway-personal-caller-_qqOw6oy.mjs";
//#region src/gateway/server-methods/gateway-client-identity.ts
function isGatewayClientProfilePending(e) {
return Boolean(e?.authenticatedGitHubIdentitySync && !e.authenticatedUserProfile);
}
/** A GitHub quota failure names its cause and carries GitHub's reset deadline. */
function authenticatedProfileUnavailableError(e) {
const t = e instanceof Error && e.name === "ControlUiGitHubError" && e instanceof gitHubPublicApi.ControlUiGitHubError && e.statusCode === 429;
return errorShape(ErrorCodes.UNAVAILABLE, t ? "GitHub is rate limiting profile verification. Retry shortly; if this continues, ask a gateway administrator to check the GitHub API credential." : "Authenticated profile verification is unavailable. Retry shortly; if this continues, contact a gateway administrator.", {
retryable: true,
retryAfterMs: t && e.retryAfterMs || 1e3,
details: { code: ConnectErrorDetailCodes.AUTHENTICATED_PROFILE_UNAVAILABLE }
});
}
async function authorizeAuthenticatedProfileForMethod(e) {
const t = e.sessionScope !== void 0;
const sessionProfileError = () => {
const n = resolveGatewayOperatorRoleActor(e.client);
return t && (n?.kind !== "operator" || !n.profileId.trim()) ? errorShape(ErrorCodes.FORBIDDEN, "Session-scoped access requires a verified user profile.") : null;
};
const n = e.client?.authenticatedGitHubIdentitySync;
if (!n || e.client?.authenticatedUserProfile?.profileId.trim()) return sessionProfileError();
if (!t && !e.requiresProfile()) return null;
if (isArtifactPreservingStateRead()) return authenticatedProfileUnavailableError();
try {
await n();
} catch (e) {
return authenticatedProfileUnavailableError(e);
}
return e.client?.authenticatedUserProfile?.profileId.trim() ? sessionProfileError() : authenticatedProfileUnavailableError();
}
function gatewayClientSenderFields(e) {
if (e?.internal?.senderAttribution) return { sender: e.internal.senderAttribution };
const t = e?.authenticatedUserProfile;
if (t) return { sender: {
id: t.profileId,
...!e?.internal?.syntheticClient ? { identity: {
type: "profile",
id: t.profileId
} } : {},
...t.displayName ? { name: t.displayName } : {}
} };
return !e?.authenticatedGitHubIdentitySync && e?.authenticatedUserId ? { sender: { id: e.authenticatedUserId } } : {};
}
/** Returns the same durable human profile identity used for session creation attribution. */
function gatewayClientSessionCreator(e) {
const t = e?.authenticatedUserProfile;
return t ? {
type: "human",
id: t.profileId,
...t.displayName ? { label: t.displayName } : {}
} : void 0;
}
/** Authenticated ingress facts shared by chat execution and its current caller controls. */
function resolveChatSendCallerContext(e, t = e?.connect?.client, n = INTERNAL_MESSAGE_CHANNEL) {
const r = isSyntheticGatewayCaller(e ?? null);
const i = r ? void 0 : getCommandSenderAuthority(e) ?? (() => {
if (!e || e.invalidated || e.connectionSignal?.aborted || isSyntheticGatewayCaller(e)) return;
const t = e.internal?.remoteIngressPrincipal;
if (t) {
try {
t.signal.throwIfAborted();
t.assertCurrent();
} catch {
return;
}
return t.operatorRoleActor.kind === "operator" && e.internal?.operatorRoleActor?.kind === "operator" && t.operatorRoleActor.profileId === e.internal.operatorRoleActor.profileId && t.operatorRoleActor.profileId === e.authenticatedUserProfile?.profileId ? t.operatorRoleActor.profileId : void 0;
}
return e.authenticatedUserId ? e.authenticatedUserProfile?.profileId : void 0;
});
return withCommandSenderAuthority({
Provider: INTERNAL_MESSAGE_CHANNEL,
Surface: INTERNAL_MESSAGE_CHANNEL,
OriginatingChannel: n,
ChatType: "direct",
ApprovalReviewerDeviceId: normalizeOptionalString(e?.connect?.device?.id),
...!r && !isOperatorUiClient(t) ? {
SenderId: t?.id,
SenderName: t?.displayName,
SenderUsername: t?.displayName
} : {},
GatewayClientScopes: e?.connect?.scopes ?? [],
GatewayClientCaps: e?.connect?.caps ?? []
}, i);
}
//#endregion
export { isGatewayClientProfilePending as a, gatewayClientSessionCreator as i, authorizeAuthenticatedProfileForMethod as n, resolveChatSendCallerContext as o, gatewayClientSenderFields as r, authenticatedProfileUnavailableError as t };