openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
143 lines (142 loc) • 5.78 kB
JavaScript
import { a as getRuntimeConfigSnapshot } from "./runtime-snapshot-WbwbY5b7.mjs";
import { i as loadBundledPluginPublicSurfaceModuleSyncCore, n as createLazyFacadeObjectValue } from "./facade-loader-08DBXZOS.mjs";
import { f as isTrustedSecretSurfaceUnavailableError, n as SecretSurfaceUnavailableError, r as assertSecretOwnerAvailable } from "./runtime-degraded-state-BSnwk5PQ.mjs";
import { a as resolveConfiguredGitHubHost, i as resolveConfiguredGitHubApiBaseUrl } from "./github-host-CjR25cOO.mjs";
//#region src/gateway/github-public-api.ts
const CONTROL_UI_GITHUB_CREDENTIAL_UNAVAILABLE_MESSAGE = "The configured Control UI GitHub credential is unavailable. Check gateway.controlUi.github.token and its host binding, then retry.";
/** Host credential selection uses canonical config and degradation state. */
function githubApiToken(e = process.env, t = getRuntimeConfigSnapshot(), n) {
const r = n ?? resolveConfiguredGitHubHost(t);
const i = t?.gateway?.controlUi?.github?.token;
const a = t?.gateway?.controlUi?.github?.host?.trim().toLowerCase() || "github.com";
if (i !== void 0 && (n === void 0 || a === r)) {
if (a !== r) throw new SecretSurfaceUnavailableError({
ownerKind: "capability",
ownerId: "control-ui-github",
state: "unavailable",
paths: ["gateway.controlUi.github.host"],
refKeys: [],
reason: "service credential host does not match gateway.github.host"
});
assertSecretOwnerAvailable("capability", "control-ui-github");
const e = typeof i === "string" ? i.trim() : "";
if (!e) throw new SecretSurfaceUnavailableError({
ownerKind: "capability",
ownerId: "control-ui-github",
state: "unavailable",
paths: ["gateway.controlUi.github.token"],
refKeys: [],
reason: "secret reference was not materialized by the active runtime"
});
return e;
}
return r === "github.com" ? e.GH_TOKEN?.trim() || e.GITHUB_TOKEN?.trim() || void 0 : void 0;
}
function hasConfiguredGitHubApiCredential(e, t) {
return t.gateway?.controlUi?.github?.token !== void 0 && (t.gateway.controlUi.github.host?.trim().toLowerCase() || "github.com") === resolveConfiguredGitHubHost(t) || resolveConfiguredGitHubHost(t) === "github.com" && Boolean(e.GH_TOKEN?.trim() || e.GITHUB_TOKEN?.trim());
}
const gitHubPublicApi = createLazyFacadeObjectValue(() => {
const e = loadBundledPluginPublicSurfaceModuleSyncCore({
dirName: "github",
artifactBasename: "api.js"
});
const resolveScope = (t = process.env, n) => {
const r = getRuntimeConfigSnapshot();
const i = resolveConfiguredGitHubHost(r);
const a = n ?? i;
if (a !== i && a !== "github.com") throw new e.ControlUiGitHubError(409, "Repository GitHub host changed; restore its configuration and retry");
const o = a === i ? resolveConfiguredGitHubApiBaseUrl(r) : "https://api.github.com";
return {
token: a === i ? githubApiToken(t, r) : void 0,
apiBaseUrl: o,
apiScope: `${a}:${o}`
};
};
const resolvePublicScope = () => resolveConfiguredGitHubHost(getRuntimeConfigSnapshot()) === "github.com" ? resolveScope() : {
token: void 0,
apiScope: "public"
};
const resolveReadIdentity = (t) => {
if (t) return resolveConfiguredGitHubHost(getRuntimeConfigSnapshot()) === "github.com" ? t : {
...t,
token: void 0,
cacheScope: "public:anonymous"
};
const n = resolvePublicScope();
const assertSelected = () => {
const t = resolvePublicScope();
if (t.apiScope !== n.apiScope || t.token !== n.token) throw new e.ControlUiGitHubError(409, "GitHub credential changed; retry the request");
};
return {
token: n.token,
cacheScope: `${n.apiScope}:${e.githubApiCredentialCacheScope(n.token)}`,
optionalAuth: true,
assertSelected,
revalidate: async () => assertSelected()
};
};
const resolveReadRequestIdentity = (e, t) => {
const n = resolveReadIdentity(e);
return t ? {
...n,
signal: t.signal,
assertSelected() {
t.assertActive();
n.assertSelected();
},
async revalidate() {
t.assertActive();
await n.revalidate();
t.assertActive();
}
} : n;
};
return {
...e,
get GITHUB_API_BASE_URL() {
return resolveConfiguredGitHubApiBaseUrl(getRuntimeConfigSnapshot());
},
get GITHUB_GRAPHQL_URL() {
return e.resolveGitHubApiUrls(getRuntimeConfigSnapshot()?.gateway?.github?.apiBaseUrl).graphqlUrl;
},
fetchGitHubApi(...t) {
t[8] ??= resolveConfiguredGitHubApiBaseUrl(getRuntimeConfigSnapshot());
return e.fetchGitHubApi(...t);
},
fetchGitHubJsonPage(t, n, r, i) {
return e.fetchGitHubJsonPage(t, n, r, {
...i,
apiBaseUrl: i?.apiBaseUrl ?? resolveConfiguredGitHubApiBaseUrl(getRuntimeConfigSnapshot())
});
},
fetchGitHubJson(...t) {
t[4] ??= resolveConfiguredGitHubApiBaseUrl(getRuntimeConfigSnapshot());
return e.fetchGitHubJson(...t);
},
resolveGitHubApiCredentialScope(t, n) {
const { token: r, apiBaseUrl: i, apiScope: a } = resolveScope(t, n);
return {
token: r,
apiBaseUrl: i,
cacheScope: `${a}:${e.githubApiCredentialCacheScope(r)}`
};
},
formatControlUiGitHubPreviewError(t) {
return isTrustedSecretSurfaceUnavailableError(t) ? {
message: CONTROL_UI_GITHUB_CREDENTIAL_UNAVAILABLE_MESSAGE,
retryable: false
} : e.formatControlUiGitHubPreviewError(t);
},
loadControlUiGitHubPreview(t, n, r, i) {
return e.loadControlUiGitHubPreview(t, resolveReadIdentity(n), r, i);
},
searchPublicGitHubItems(t, n, r, i) {
return e.searchPublicGitHubItems(t, resolveReadRequestIdentity(n, i), r);
},
loadGitHubDetail(t, n, r, i, a, o) {
return e.loadGitHubDetail(t, resolveReadRequestIdentity(n, a), r, i, o);
}
};
});
//#endregion
export { hasConfiguredGitHubApiCredential as i, gitHubPublicApi as n, githubApiToken as r, CONTROL_UI_GITHUB_CREDENTIAL_UNAVAILABLE_MESSAGE as t };