openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
101 lines (100 loc) • 3.33 kB
JavaScript
import { n as getRuntimeConfig } from "./io.runtime-B9iJRs3w.js";
import { t as ADMIN_SCOPE } from "./operator-scopes-Dw7Gu2cA.js";
import "./io-bdCzpGWJ.js";
import { r as authorizeOperatorScopesForRequiredScope } from "./method-scopes-K6J_UQGL.js";
import { n as resolveRequestedSessionAgentId } from "./session-request-agent-CCRSEGCB.js";
import { r as loadGatewaySessionEntry } from "./session-utils-store-CInT2loy.js";
import "./session-utils-Cai0_C6U.js";
import { n as killSubagentRunAdmin } from "./subagent-control-Cm3eFdYX.js";
import { a as authorizeGatewayHttpRequestOrReply, v as resolveTrustedHttpOperatorScopes } from "./http-auth-utils-C3lb4QXY.js";
import { c as sendMethodNotAllowed, l as sendMissingScopeForbidden, o as sendInvalidRequest, s as sendJson } from "./http-common-BaZaosnr.js";
import "./http-utils-BHgXp7Zb.js";
//#region src/gateway/session-kill-http.ts
function resolveSessionKeyFromPath(pathname) {
const match = pathname.match(/^\/sessions\/([^/]+)\/kill$/);
if (!match) return { matched: false };
try {
const decoded = decodeURIComponent(match[1] ?? "").trim();
if (!decoded) return {
error: "invalid-session-key",
matched: true
};
return {
matched: true,
sessionKey: decoded
};
} catch {
return {
error: "invalid-session-key",
matched: true
};
}
}
async function handleSessionKillHttpRequest(req, res, opts) {
const cfg = getRuntimeConfig();
const url = new URL(req.url ?? "/", "http://localhost");
const sessionKeyResolution = resolveSessionKeyFromPath(url.pathname);
if (!sessionKeyResolution.matched) return false;
if ("error" in sessionKeyResolution) {
sendInvalidRequest(res, "invalid session key");
return true;
}
const { sessionKey } = sessionKeyResolution;
if (req.method !== "POST") {
sendMethodNotAllowed(res, "POST");
return true;
}
const requestAuth = await authorizeGatewayHttpRequestOrReply({
req,
res,
auth: opts.auth,
trustedProxies: opts.trustedProxies ?? cfg.gateway?.trustedProxies,
allowRealIpFallback: opts.allowRealIpFallback ?? cfg.gateway?.allowRealIpFallback,
rateLimiter: opts.rateLimiter
});
if (!requestAuth) return true;
const requestedScopes = resolveTrustedHttpOperatorScopes(req, requestAuth);
const scopeAuth = authorizeOperatorScopesForRequiredScope(ADMIN_SCOPE, requestedScopes);
if (!scopeAuth.allowed) {
sendMissingScopeForbidden(res, scopeAuth.missingScope);
return true;
}
const requestedAgent = resolveRequestedSessionAgentId(cfg, sessionKey, url.searchParams.get("agentId") ?? void 0);
if (!requestedAgent.ok) {
sendInvalidRequest(res, requestedAgent.error.message);
return true;
}
const { entry, canonicalKey } = loadGatewaySessionEntry(sessionKey, { agentId: requestedAgent.agentId });
if (!entry) {
sendJson(res, 404, {
ok: false,
error: {
type: "not_found",
message: `Session not found: ${sessionKey}`
}
});
return true;
}
const result = await killSubagentRunAdmin({
cfg,
sessionKey: canonicalKey,
agentId: requestedAgent.agentId
});
if (result.found && result.error) {
sendJson(res, 503, {
ok: false,
error: {
type: "unavailable",
message: result.error
}
});
return true;
}
sendJson(res, 200, {
ok: true,
killed: result.killed
});
return true;
}
//#endregion
export { handleSessionKillHttpRequest };