openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
152 lines (151 loc) • 6.54 kB
JavaScript
import { O as tryResolveAmbientOwnerAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { d as ModelAccountConnectAuthorityError } from "./openclaw-state-worker-error-C8y1JGQN.mjs";
import { a as hasGatewayClientCap, t as GATEWAY_CLIENT_CAPS } from "./client-info-Cm1BcrMm.mjs";
import { Gc as validateModelsListParams } from "./src-BX8OAe1J.mjs";
import { a as READ_SCOPE, o as SESSION_READ_SCOPE } from "./operator-scopes-CULmYfyY.mjs";
import { o as roleScopesAllow } from "./operator-scope-compat-CpFHu-FC.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { n as authorizeCurrentOperatorRoleScopes } from "./operator-role-policy-DhcqVpmG.mjs";
import { t as SessionMutationAuthorizationChangedError } from "./session-mutation-authorization-error-0K0ZVV7q.mjs";
import { t as createPreparedReadHandler } from "./prepared-read-BXLtRH-W.mjs";
import { o as projectModelFastModeCatalog, r as prepareOperatorModelPresentation } from "./operator-model-presentation-BAAXja2o.mjs";
import { i as readSessionRuntimeOwnershipAsync } from "./session-utils-model-selection-BGYqZws_.mjs";
import { t as sessionModelRevision } from "./session-model-revision-5CkEoior.mjs";
import { c as applyRemoteModelCatalogUpdate } from "./prepared-model-runtime-Bbo9uBdt.mjs";
import { a as isPreparedModelRuntimeMissingOwnerError } from "./prepared-model-runtime.errors-BRsU9-dm.mjs";
import { r as getPublishedPreparedModelCatalogOwnerSnapshot } from "./prepared-model-catalog-DSktrHGK.mjs";
import { t as assertValidParams } from "./validation-DwNk6A-B.mjs";
import { t as resolveAgentIdOrRespondError } from "./agent-id-shared-YyMfd879.mjs";
import { t as preparePersonalModelAccountSelection } from "./users-model-account-access-qMuKU1sR.mjs";
import { n as resolveChatMetadataReadParams } from "./chat-metadata-handler-CPaDSq6_.mjs";
import { i as projectSessionModelCatalog } from "./chat-metadata-session-projection-Dqm83JVm.mjs";
import { r as UnknownModelCatalogProviderError, t as buildModelsListResult } from "./models-list-result-BOk0LqJ-.mjs";
//#region src/gateway/server-methods/models.ts
const modelsHandlers = { "models.list": createPreparedReadHandler(async (e) => {
const { params: t, respond: n, context: r, client: i } = e;
if (!assertValidParams(t, validateModelsListParams, "models.list", n)) return;
let a;
let o;
try {
const s = Boolean(t.sessionKey || t.authProfileId);
const c = !t.sessionKey && t.authProfileId ? await preparePersonalModelAccountSelection(e, t.authProfileId, SESSION_READ_SCOPE) : void 0;
a = s ? await resolveChatMetadataReadParams(e, t, c) : void 0;
if (s && !a) return;
const l = r.getRuntimeConfig();
const u = a ?? resolveAgentIdOrRespondError({
rawAgentId: t.agentId ?? tryResolveAmbientOwnerAgentId(l),
respond: n,
cfg: l
});
if (!u) return;
if (!a) {
const t = authorizeCurrentOperatorRoleScopes(i, l);
if (t) {
n(false, void 0, t);
return;
}
const r = i?.connect.scopes ?? [];
if (roleScopesAllow({
role: "operator",
requestedScopes: ["operator.sessions.read"],
allowedScopes: r
}) && !roleScopesAllow({
role: "operator",
requestedScopes: ["operator.read"],
allowedScopes: r
})) {
a = await resolveChatMetadataReadParams(e, { agentId: u.agentId });
if (!a) return;
}
}
o = a ?? await resolveChatMetadataReadParams(e, { agentId: u.agentId });
if (!o) return;
const d = o;
const assertCurrent = () => {
d.draftAccountSelection?.assertCurrent();
d.assertCurrent?.();
};
const { ensureGatewayPreparedModelRuntimeReady: f } = await import("./prepared-model-runtime-D6x7ghYr.mjs");
assertCurrent();
await f({ agentId: u.agentId });
assertCurrent();
if (t.refresh !== true) {
const e = getPublishedPreparedModelCatalogOwnerSnapshot({
agentId: u.agentId,
config: l
});
e?.recheckNativeLogin?.();
if (!t.preparedOnly && t.view !== "provider-config") {
e?.loadFullModelCatalog?.({
changedOnly: true,
...t.provider ? { providerIds: [t.provider] } : {}
}).catch(() => void 0);
e?.loadNativeModelCatalog?.().catch(() => void 0);
}
}
return {
assertCurrent,
release: d.release,
run: async (e) => {
const n = hasGatewayClientCap(i?.connect.caps, GATEWAY_CLIENT_CAPS.MODEL_SELECTION_POLICY);
const listParams = () => ({
agentId: u.agentId,
params: t,
includeManualSelection: n,
requesterProfileId: d.requesterProfileId,
readScope: a
});
const o = (t.refresh !== true ? await r.readPreparedModelsList?.(listParams()) : void 0) ?? await buildModelsListResult({
source: {
kind: "gateway",
context: r
},
...listParams(),
publicationScope: d
});
const s = a && t.view !== "provider-config" ? await readSessionRuntimeOwnershipAsync({
...a,
config: r.getRuntimeConfig(),
assertCurrent
}) : void 0;
const publish = () => {
assertCurrent();
const n = r.getRuntimeConfig();
const c = t.view !== "provider-config" ? {
...o,
...a?.sessionKey ? { sessionModelRevision: sessionModelRevision(a.sessionEntry, a.workerInference, s ?? null) } : {},
models: projectSessionModelCatalog(a ?? { agentId: u.agentId }, o.models, n, s)
} : o;
const l = prepareOperatorModelPresentation({
cfg: n,
policyConfig: r.getCommittedRuntimeConfig?.() ?? n,
client: i
})?.forAgent(u.agentId, c.models);
e(true, projectModelFastModeCatalog(l ? l.catalog(c) : c, i), void 0);
};
if (d.withCurrent) await d.withCurrent(publish);
else publish();
if (t.refresh === true) Promise.resolve().then(() => applyRemoteModelCatalogUpdate(r.getRuntimeConfig)).catch((e) => {
r.logGateway.warn("remote model catalog adoption failed", { error: String(e) });
});
}
};
} catch (e) {
(o ?? a)?.release?.();
throw e;
}
}, (e, { respond: t }) => {
let n;
if (e instanceof UnknownModelCatalogProviderError) n = errorShape(ErrorCodes.INVALID_REQUEST, e.message);
else if (e instanceof SessionMutationAuthorizationChangedError) n = e.error;
else if (isPreparedModelRuntimeMissingOwnerError(e)) n = errorShape(ErrorCodes.UNAVAILABLE, e.message, {
retryable: true,
retryAfterMs: 0
});
else if (e instanceof ModelAccountConnectAuthorityError) n = errorShape(ErrorCodes.FORBIDDEN, e.message);
else throw e;
t(false, void 0, n);
}) };
//#endregion
export { modelsHandlers };