openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
149 lines (148 loc) • 6.08 kB
JavaScript
import { l as isRecord } from "./record-coerce-CVPuDqcc.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { n as detectMime } from "./mime-D2wVBzpa.mjs";
import { r as fetchWithSsrFGuard } from "./fetch-guard-BLMw2n18.mjs";
import "./string-coerce-runtime-BwAZZDf5.mjs";
import "./ssrf-runtime-B6q6NM4E.mjs";
import "./media-mime-8fD2xhws.mjs";
import { t as dispatchGatewayMethod } from "./gateway-method-runtime-Bu-SKY1m.mjs";
import "./gateway-runtime-Du0olVch.mjs";
import { l as parseGitHubTarget, o as parseGitHubLinkParams, r as githubTargetUrl } from "./targets-CFNRMYXS.mjs";
import { d as formatControlUiGitHubPreviewError, h as readBoundedResponse, s as discardResponse, t as ControlUiGitHubError } from "./github-api-C6HIcRTo.mjs";
import { n as githubPreviewView } from "./view-model-Bu-ryg_k.mjs";
//#region extensions/github/src/preview-contract.ts
/** Validate the untyped dispatch payload before projecting it into a generic reader. */
function isControlUiGitHubPreview(e) {
if (!isRecord(e) || !parseGitHubTarget(e)) return false;
return [
"createdAt",
"login",
"state",
"title",
"updatedAt"
].every((t) => typeof e[t] === "string") && [
"avatarDataUrl",
"closedAt",
"mergedAt",
"stateReason"
].every((t) => e[t] === void 0 || typeof e[t] === "string") && [
"additions",
"changedFiles",
"comments",
"deletions",
"coAuthorCount"
].every((t) => e[t] === void 0 || typeof e[t] === "number" && Number.isFinite(e[t])) && (e.stale === void 0 || typeof e.stale === "boolean") && (e.draft === void 0 || typeof e.draft === "boolean") && (e.coAuthors === void 0 || Array.isArray(e.coAuthors) && e.coAuthors.every((e) => isRecord(e) && typeof e.login === "string" && (e.avatarDataUrl === void 0 || typeof e.avatarDataUrl === "string")));
}
//#endregion
//#region extensions/github/src/handlers.ts
const IMAGE_HOSTS = [
"github.com",
"user-images.githubusercontent.com",
"private-user-images.githubusercontent.com",
"github-production-user-asset-6210df.s3.amazonaws.com"
];
const ATTACHMENT_PATH = /^\/user-attachments\/assets\/[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}$/i;
const RASTER_TYPES = /* @__PURE__ */ new Set([
"image/png",
"image/jpeg",
"image/gif",
"image/webp"
]);
const MAX_IMAGE_BYTES = 2097152;
function isImageUrl(e, t) {
return e.protocol === "https:" && !e.username && !e.password && !e.port && IMAGE_HOSTS.includes(e.hostname) && (e.hostname !== "github.com" || ATTACHMENT_PATH.test(e.pathname)) && (!t || !e.hostname.endsWith(".s3.amazonaws.com"));
}
async function loadGitHubImage(e) {
const { response: t, release: n } = await fetchWithSsrFGuard({
url: e,
requireHttps: true,
timeoutMs: 15e3,
maxRedirects: 3,
capture: false,
policy: { hostnameAllowlist: IMAGE_HOSTS },
resolveDispatcherPolicy: (e) => {
if (!isImageUrl(e, false)) throw new Error("GitHub image redirect is not permitted");
},
init: {
credentials: "omit",
headers: { Accept: [...RASTER_TYPES].join(",") }
}
});
try {
if (!t.ok) throw new Error("GitHub image is unavailable");
const n = await readBoundedResponse(t, MAX_IMAGE_BYTES);
const r = await detectMime({ buffer: n });
if (!r || !RASTER_TYPES.has(r)) throw new Error("GitHub attachment is not a supported image");
return {
url: e,
dataUrl: `data:${r};base64,${n.toString("base64")}`
};
} finally {
await discardResponse(t);
await n();
}
}
async function handleGitHubRequest(e, { params: t, respond: n }, r) {
const i = parseGitHubLinkParams(t, r);
if (!i || r && (typeof t.sessionKey !== "string" || !t.sessionKey.trim()) || e === "github.preview" && i.section !== void 0) {
n(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid " + e + " params"));
return;
}
try {
const a = await dispatchGatewayMethod(e === "github.preview" ? "controlUi.githubPreview" : "controlUi.githubDetail", {
...i.target,
...r ? {
githubHost: r,
sessionKey: t.sessionKey
} : {},
...i.agentId ? { agentId: i.agentId } : {},
...i.refresh ? { refresh: true } : {},
...i.section ? { section: i.section } : {}
});
if (!a.ok) {
n(false, a.payload, a.error, a.meta);
return;
}
if (e === "github.preview") {
if (!isControlUiGitHubPreview(a.payload) || githubTargetUrl(a.payload, r).toLowerCase() !== githubTargetUrl(i.target, r).toLowerCase()) throw new ControlUiGitHubError(502, "GitHub preview returned an invalid response");
n(true, {
...githubPreviewView(a.payload, r),
url: i.url
}, void 0, a.meta);
} else {
const e = a.payload;
if (!isRecord(e) || typeof e.title !== "string" || typeof e.body !== "string" || typeof e.url !== "string" || e.url.toLowerCase() !== githubTargetUrl(i.target, r).toLowerCase()) throw new ControlUiGitHubError(502, "GitHub document returned a different resource");
n(true, {
...e,
url: i.url,
filesExpanded: i.filesExpanded
}, void 0, a.meta);
}
} catch (e) {
const { message: t, ...r } = formatControlUiGitHubPreviewError(e);
n(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, t, r));
}
}
const githubHandlers = {
"github.image": async ({ params: e, respond: t }) => {
const n = isRecord(e) && typeof e.url === "string" && e.url.length <= 8192 ? URL.parse(e.url) : null;
if (!n || !isImageUrl(n, true)) {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid github.image params"));
return;
}
try {
t(true, await loadGitHubImage(n.href), void 0);
} catch {
t(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "GitHub image is unavailable"));
}
},
"github.preview": (e) => handleGitHubRequest("github.preview", e),
"github.detail": (e) => handleGitHubRequest("github.detail", e)
};
/** Separate from the public/profile-independent RPC; the host owns session admission. */
function handleSessionGitHubRequest(e, t, n) {
return handleGitHubRequest(e === "preview" ? "github.preview" : "github.detail", n, t);
}
//#endregion
export { githubHandlers, handleSessionGitHubRequest };