UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

123 lines (122 loc) 6.95 kB
import { n as normalizeAgentId } from "./agent-id-CeT3w4ap.js"; import "./session-key-BnWWjqNc.js"; import { t as ErrorCodes } from "./gateway-error-details-w0nAGBBp.js"; import { Dr as validateSessionGitHubPublishParams, Er as validateSessionGitHubOptionsParams, Or as validateSessionGitHubStatusParams, Tr as validateSessionGitHubConfirmParams } from "./src-BiL5aQto.js"; import { d as errorShape } from "./error-codes-Bo8q2D1o.js"; import { r as getGatewayToolCallerIdentity } from "./gateway-caller-context-BxVUFvkR.js"; import { n as resolveRequestedSessionAgentId } from "./session-request-agent-CCRSEGCB.js"; import { i as loadGatewaySessionEntryReadOnly } from "./session-utils-store-CInT2loy.js"; import "./session-utils-Cai0_C6U.js"; import { H as SessionMutationAuthorizationChangedError } from "./session-sharing-B7MI8hNo.js"; import { n as defineValidatedGatewayMethod } from "./validation-pzrlzFvo.js"; import { i as prepareCurrentGitHubPublicationIdentity, s as GitHubPublicationKnownFailure } from "./github-publication-availability-BsRK-v3w.js"; import { n as preparePersonalGitHubAction, r as preparePersonalGitHubSessionAction, t as prepareGitHubPublicationOptionsRead } from "./github-personal-authorization-DFNcwehW.js"; //#region src/gateway/server-methods/sessions-github.ts const sessionGitHubFailureMessages = { "sessions.github.publish": "GitHub publication request failed", "sessions.github.options": "GitHub publication options are unavailable.", "sessions.github.status": "GitHub publication status is unavailable.", "sessions.github.confirm": "GitHub publication confirmation failed." }; function defineSessionGitHubMethod(...[method, validate, handler]) { return defineValidatedGatewayMethod(method, validate, async (options) => { const { agentId, sessionKey } = options.params; const key = sessionKey ?? getGatewayToolCallerIdentity()?.sessionKey; if (agentId !== void 0 && key) { const owner = resolveRequestedSessionAgentId(options.context.getRuntimeConfig(), key, agentId); if (!owner.ok) { options.respond(false, void 0, owner.error); return; } } try { return await handler(options); } catch (error) { const publishing = method === "sessions.github.publish"; if (publishing && error instanceof SessionMutationAuthorizationChangedError) throw error; options.respond(false, void 0, errorShape(publishing ? ErrorCodes.UNAVAILABLE : ErrorCodes.FORBIDDEN, error instanceof Error ? error.message : sessionGitHubFailureMessages[method], publishing && error instanceof GitHubPublicationKnownFailure && "idempotencyKey" in options.params && error.rejection?.idempotencyKey === options.params.idempotencyKey ? { details: error.rejection } : void 0)); } }); } const sessionsGitHubHandlers = { "sessions.github.publish": defineSessionGitHubMethod("sessions.github.publish", validateSessionGitHubPublishParams, async (options) => { const { params, respond, context, sessionMutationAuthorization } = options; const coordinator = context.githubPublicationService; if (!coordinator) { respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, "GitHub publication is unavailable on this Gateway")); return; } const caller = getGatewayToolCallerIdentity(); const sessionKey = caller?.sessionKey ?? params.sessionKey; if (!sessionKey || caller && params.sessionKey && params.sessionKey !== caller.sessionKey || caller && params.agentId && normalizeAgentId(params.agentId) !== normalizeAgentId(caller.agentId)) { respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "GitHub publication session is invalid")); return; } const agentId = caller?.agentId ?? params.agentId; if (params.selection?.source === "personal") { if (!params.sessionKey) throw new Error("My GitHub publication requires an explicit session."); const action = preparePersonalGitHubSessionAction(options, { sessionKey: params.sessionKey, agentId }); const result = await coordinator.requestPersonalForSession(params, action); action.assertCurrent(); respond(true, result); return; } const loaded = loadGatewaySessionEntryReadOnly(sessionKey, agentId ? { agentId } : void 0); if (!loaded.entry?.sessionId) { respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "GitHub publication session was not found")); return; } sessionMutationAuthorization?.assertCurrent(); const result = await coordinator.requestForSession({ ...params, sessionKey: loaded.canonicalKey, agentId: caller?.agentId ?? loaded.agentId, ...caller?.operationalRunInstance?.runId ? { expectedRunId: caller.operationalRunInstance.runId } : {}, ...sessionMutationAuthorization ? { assertCurrent: sessionMutationAuthorization.assertCurrent } : {} }); sessionMutationAuthorization?.assertCurrent(); respond(true, result); }), "sessions.github.options": defineSessionGitHubMethod("sessions.github.options", validateSessionGitHubOptionsParams, async (options) => { const read = prepareGitHubPublicationOptionsRead(options, options.params); let shared = null; try { const identity = await prepareCurrentGitHubPublicationIdentity(read.session.agentId); shared = { source: identity.source, accountId: identity.account.accountId, login: identity.account.login }; } catch {} const service = options.context.githubOAuthService?.personal; if (read.personal.kind === "eligible" && (!service || !options.context.githubPublicationService)) throw new Error("GitHub connections are unavailable; retry after Gateway startup."); const action = read.personal.kind === "eligible" ? read.personal.action : null; const personal = action ? await service.status(action) : null; const session = read.currentSession(); options.respond(true, { personal, shared, pendingPersonal: action ? options.context.githubPublicationService.personalPending(action, session) : null }); }), "sessions.github.status": defineSessionGitHubMethod("sessions.github.status", validateSessionGitHubStatusParams, (options) => { const action = preparePersonalGitHubAction(options); const { session } = prepareGitHubPublicationOptionsRead(options, options.params); const service = options.context.githubPublicationService; if (!service) throw new Error("GitHub publication is unavailable."); options.respond(true, service.personalStatus(action, session, options.params.requestId)); }), "sessions.github.confirm": defineSessionGitHubMethod("sessions.github.confirm", validateSessionGitHubConfirmParams, async (options) => { const action = preparePersonalGitHubSessionAction(options, options.params); const service = options.context.githubPublicationService; if (!service) throw new Error("GitHub publication is unavailable."); const result = await service.confirmPersonal(options.params, action); action.assertCurrent(); options.respond(true, result); }) }; //#endregion export { sessionsGitHubHandlers };