openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
114 lines (113 loc) • 5.29 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-6kR-86XC.mjs";
import { C as tryResolveAmbientOwnerAgentId } from "./agent-scope-config-BU4qc9bA.mjs";
import { o as SESSION_READ_SCOPE } from "./operator-scopes-D-CL26h0.mjs";
import { a as hasGatewayClientCap, t as GATEWAY_CLIENT_CAPS } from "./client-info-S_xwnr2o.mjs";
import { wn as validateModelsListParams } from "./src-D2gvuSyP.mjs";
import { a as roleScopesAllow } from "./operator-scope-compat-Ci6GBcmU.mjs";
import { t as ErrorCodes } from "./gateway-error-details-DIefe7Zy.mjs";
import { f as errorShape } from "./error-codes-DI20vDEB.mjs";
import { t as authorizeCurrentOperatorRoleScopes } from "./operator-role-policy-C96xz3qI.mjs";
import { r as PreparedModelRuntimePublicationSupersededError } from "./prepared-model-runtime.errors-DbFetllN.mjs";
import { f as refreshExpiredPreparedModelCatalog } from "./prepared-model-catalog-0uZANRjz.mjs";
import { t as SessionMutationAuthorizationChangedError } from "./session-mutation-authorization-error-CCz8QGWh.mjs";
import { n as prepareOperatorModelPresentation } from "./operator-model-presentation-CxUq2uug.mjs";
import { t as assertValidParams } from "./validation-B7TobU6-.mjs";
import { t as ModelAccountConnectAuthorityError } from "./model-account-connect-DtCW1UJq.mjs";
import { t as resolveAgentIdOrRespondError } from "./agent-id-shared-CCm6C9lW.mjs";
import { n as resolveChatMetadataReadParams } from "./chat-metadata-handler-CVXHHB3o.mjs";
import { t as preparePersonalModelAccountSelection } from "./users-model-account-access-DIdI_pt1.mjs";
import { i as projectSessionModelCatalog } from "./chat-metadata-session-projection-DHurZKU-.mjs";
import { i as UnknownModelCatalogProviderError, t as buildModelsListResult } from "./models-list-result-Ce5xGu1y.mjs";
//#region src/gateway/server-methods/models.ts
const modelsHandlers = { "models.list": async (options) => {
const { params, respond, context, client } = options;
if (!assertValidParams(params, validateModelsListParams, "models.list", respond)) return;
let scope;
let publicationScope;
try {
const scoped = Boolean(params.sessionKey || params.authProfileId);
const draftAccountSelection = !params.sessionKey && params.authProfileId ? await preparePersonalModelAccountSelection(options, params.authProfileId, SESSION_READ_SCOPE) : void 0;
scope = scoped ? resolveChatMetadataReadParams(options, params, draftAccountSelection) : void 0;
if (scoped && !scope) return;
const cfg = context.getRuntimeConfig();
const resolved = scope ?? resolveAgentIdOrRespondError({
rawAgentId: params.agentId ?? tryResolveAmbientOwnerAgentId(cfg),
respond,
cfg,
normalize: normalizeOptionalString
});
if (!resolved) return;
if (!scope) {
const roleError = authorizeCurrentOperatorRoleScopes(client, cfg);
if (roleError) {
respond(false, void 0, roleError);
return;
}
const scopes = client?.connect.scopes ?? [];
if (roleScopesAllow({
role: "operator",
requestedScopes: ["operator.sessions.read"],
allowedScopes: scopes
}) && !roleScopesAllow({
role: "operator",
requestedScopes: ["operator.read"],
allowedScopes: scopes
})) {
scope = resolveChatMetadataReadParams(options, { agentId: resolved.agentId });
if (!scope) return;
}
}
publicationScope = scope ?? resolveChatMetadataReadParams(options, { agentId: resolved.agentId });
if (!publicationScope) return;
if (params.refresh !== true) refreshExpiredPreparedModelCatalog({
agentId: resolved.agentId,
config: cfg
});
const result = await buildModelsListResult({
source: {
kind: "gateway",
context
},
agentId: resolved.agentId,
params,
includeManualSelection: hasGatewayClientCap(client?.connect.caps, GATEWAY_CLIENT_CAPS.MODEL_SELECTION_POLICY),
requesterProfileId: publicationScope.requesterProfileId,
...scope ? { readScope: scope } : {}
});
publicationScope.draftAccountSelection?.assertCurrent();
publicationScope.assertCurrent?.();
const currentConfig = context.getRuntimeConfig();
const projected = scope && params.view !== "provider-config" ? {
...result,
models: projectSessionModelCatalog(scope, result.models, currentConfig)
} : result;
const policy = prepareOperatorModelPresentation({
cfg: currentConfig,
policyConfig: context.getCommittedRuntimeConfig?.() ?? currentConfig,
client
})?.forAgent(resolved.agentId, projected.models);
respond(true, policy ? policy.catalog(projected) : projected, void 0);
} catch (error) {
if (error instanceof UnknownModelCatalogProviderError) {
respond(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, error.message));
return;
}
if (error instanceof SessionMutationAuthorizationChangedError) {
respond(false, void 0, error.error);
return;
}
if (error instanceof PreparedModelRuntimePublicationSupersededError) {
respond(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, error.message, {
retryable: true,
retryAfterMs: 0
}));
return;
}
if (!(error instanceof ModelAccountConnectAuthorityError)) throw error;
respond(false, void 0, errorShape(ErrorCodes.FORBIDDEN, error.message));
} finally {
(publicationScope ?? scope)?.release?.();
}
} };
//#endregion
export { modelsHandlers };