openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
141 lines (140 loc) • 6.97 kB
JavaScript
import { n as getRuntimeConfig } from "./io.runtime-B9iJRs3w.js";
import "./config-Cs0XXL3x.js";
import { n as GatewayErrorDetailCodes } from "./gateway-error-details-w0nAGBBp.js";
import { i as loadGatewaySessionEntryReadOnly } from "./session-utils-store-CInT2loy.js";
import "./session-utils-Cai0_C6U.js";
import { a as getActiveSecretsRuntimeConfigSnapshot } from "./runtime-state-C4aJ8Hzz.js";
import { o as matchesPreparedGitHubPublicationIdentity, s as prepareGitHubPublicationIdentity } from "./github-tool-identity-CsNyDjEx.js";
import { l as managedWorktrees } from "./service-B1SvAvxa.js";
import { r as requestCurrentGitHubOAuthRefresh } from "./github-oauth-lifecycle-DMB5peav.js";
//#region src/gateway/github-publication-failure.ts
const identityNextAction = "Reconnect My GitHub or System GitHub in Settings → Profile → GitHub connections (agent overrides: Agents → Tools), then request publication again.";
/** An owner observed a definitive outcome; an unavailable probe is not this failure. */
var GitHubPublicationKnownFailure = class extends Error {
constructor(message, failure, rejection) {
super(message);
this.failure = failure;
this.rejection = rejection;
}
};
function rejectGitHubPublicationSelection(message, preparation) {
let rejection;
try {
if (preparation && !preparation.hasRequest()) rejection = {
code: GatewayErrorDetailCodes.GITHUB_PUBLICATION_SELECTION_REJECTED,
idempotencyKey: preparation.idempotencyKey
};
} catch {}
throw new GitHubPublicationKnownFailure(message, {
code: "identity_changed",
nextAction: identityNextAction
}, rejection);
}
var GitHubPublicationWorkspaceChangedError = class extends GitHubPublicationKnownFailure {
constructor(message) {
super(message, {
code: "workspace_changed",
nextAction: "Inspect the reconciled workspace and any recorded GitHub effects, then request a new publication after reviewing the changes."
});
}
};
function resolveGitHubPublicationFailure(error) {
if (error instanceof GitHubPublicationKnownFailure) return error.failure;
const message = error instanceof Error ? error.message : "";
if (message.includes("identity")) return {
code: message.includes("changed") ? "identity_changed" : "identity_unavailable",
nextAction: identityNextAction
};
if (message.includes("session") || message.includes("worktree owner")) return {
code: "session_changed",
nextAction: "Open the current session worktree and request publication again."
};
if (message.includes("transport configuration") || message.includes("replacement metadata")) return {
code: "workspace_changed",
nextAction: "Remove the unsupported Git transport or replacement configuration from the session worktree, then retry."
};
if (message.includes("workspace") || message.includes("branch changed")) return {
code: "workspace_changed",
nextAction: "Inspect the reconciled workspace and any recorded GitHub effects, then request a new publication after reviewing the changes."
};
if (message.includes("not a git")) return {
code: "not_git",
nextAction: "Use a session-owned Git worktree to publish."
};
if (message.includes("GitHub remote")) return {
code: "not_github",
nextAction: "Use a GitHub repository remote to publish."
};
if (message.includes("push")) return {
code: "push_rejected",
nextAction: "Check repository write access and branch drift, then retry without force-pushing."
};
if (message.includes("pull request") || message.includes("GitHub")) return {
code: "github_rejected",
nextAction: "Check pull-request permission for the effective account, then retry."
};
return {
code: "unavailable",
nextAction: "Retry after the Gateway and GitHub are available."
};
}
//#endregion
//#region src/gateway/github-publication-availability.ts
function publicationConfigSnapshot() {
const active = getActiveSecretsRuntimeConfigSnapshot();
if (active) return active;
const config = getRuntimeConfig();
return {
config,
sourceConfig: config
};
}
function assertExpectedSharedGitHubPublisher(expected, actual, preparation) {
if (actual.source === "personal" || expected && (expected.source !== actual.source || expected.accountId !== actual.accountId || expected.login.toLowerCase() !== actual.login.toLowerCase())) rejectGitHubPublicationSelection("GitHub publication identity changed; review the current shared account and try again.", preparation);
}
function currentGitHubPublicationConfig() {
return publicationConfigSnapshot().config;
}
async function prepareCurrentGitHubPublicationIdentity(agentId) {
await requestCurrentGitHubOAuthRefresh(agentId);
const snapshot = publicationConfigSnapshot();
return await prepareGitHubPublicationIdentity({
config: snapshot.config,
sourceConfig: snapshot.sourceConfig,
agentId
});
}
function matchesCurrentGitHubPublicationIdentity(params) {
return matchesPreparedGitHubPublicationIdentity({
config: currentGitHubPublicationConfig(),
...params
});
}
function resolveGitHubPublicationWorktreeOwner(params) {
const loaded = loadGatewaySessionEntryReadOnly(params.sessionKey, { agentId: params.agentId });
const entry = loaded.entry;
const worktree = managedWorktrees.findLiveByOwner("session", loaded.canonicalKey);
if (loaded.agentId !== params.agentId || loaded.canonicalKey !== params.sessionKey || entry?.sessionId !== params.sessionId || entry.archivedAt !== void 0 || !entry.worktree?.id || !worktree || worktree.id !== entry.worktree.id || worktree.ownerKind !== "session" || worktree.ownerId !== loaded.canonicalKey || worktree.branch !== entry.worktree.branch || worktree.repoRoot !== entry.worktree.repoRoot) throw new Error("GitHub publication session worktree owner changed.");
if (params.expected && (worktree.id !== params.expected.worktreeId || worktree.repoFingerprint !== params.expected.repositoryFingerprint || worktree.branch !== params.expected.branch)) throw new GitHubPublicationWorkspaceChangedError("GitHub publication workspace authority changed.");
return {
loaded,
worktree
};
}
async function prepareGitHubPublicationAvailability(params) {
try {
if (params.assertCurrent?.() === false) return false;
resolveGitHubPublicationWorktreeOwner(params);
const identity = await prepareCurrentGitHubPublicationIdentity(params.agentId);
if (params.assertCurrent?.() === false) return false;
resolveGitHubPublicationWorktreeOwner(params);
return matchesCurrentGitHubPublicationIdentity({
agentId: params.agentId,
identity
});
} catch {
return false;
}
}
//#endregion
export { prepareGitHubPublicationAvailability as a, GitHubPublicationWorkspaceChangedError as c, prepareCurrentGitHubPublicationIdentity as i, rejectGitHubPublicationSelection as l, currentGitHubPublicationConfig as n, resolveGitHubPublicationWorktreeOwner as o, matchesCurrentGitHubPublicationIdentity as r, GitHubPublicationKnownFailure as s, assertExpectedSharedGitHubPublisher as t, resolveGitHubPublicationFailure as u };