openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
200 lines (199 loc) • 7.85 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { n as normalizeAgentId } from "./agent-id-CQjtp2ft.mjs";
import "./session-key-CqAyZeVk.mjs";
import { x as resolveSessionAgentId } from "./agent-scope-config-DmXgFGKP.mjs";
import { d as ModelAccountConnectAuthorityError } from "./openclaw-state-worker-error-C8y1JGQN.mjs";
import { gs as validateChatMetadataParams } from "./src-BX8OAe1J.mjs";
import { m as readUserProfileAliasRevision } from "./user-profile-events-BhLFevt9.mjs";
import "./agent-scope-BwB8YH6y.mjs";
import { o as SESSION_READ_SCOPE } from "./operator-scopes-CULmYfyY.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { s as operatorSessionCap, u as readOperatorRolePolicyRevision } from "./operator-role-policy-DhcqVpmG.mjs";
import { t as SessionMutationAuthorizationChangedError } from "./session-mutation-authorization-error-0K0ZVV7q.mjs";
import { r as resolveRequestedSessionAgentId } from "./session-request-agent-tfwWH53J.mjs";
import { u as hiddenSessionNotFound } from "./session-sharing-policy-3TAaxC-R.mjs";
import { r as createSessionListEntryFilter } from "./session-sharing-read-BqjuH16A.mjs";
import "./session-sharing-BIP3raq1.mjs";
import { n as prepareSessionSharingRead } from "./session-sharing-target-read-Gmjqaux8.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 { r as PreparedModelRuntimePublicationSupersededError } from "./prepared-model-runtime-admission-errors-LxrcCg4d.mjs";
import "./prepared-model-runtime.errors-BRsU9-dm.mjs";
import { t as assertValidParams } from "./validation-DwNk6A-B.mjs";
import { l as readSessionWorkerPlacementAsync } from "./session-placement-lifecycle-3XCQYoeM.mjs";
import { t as resolveAgentIdOrRespondError } from "./agent-id-shared-YyMfd879.mjs";
import { a as readWorkerPlacementIdentity } from "./placement-projector-CSR-l50W.mjs";
import { n as ChatMetadataStartingError } from "./chat-metadata-facts-DUNVwVuX.mjs";
import { t as prepareChatMetadataSessionRead } from "./chat-metadata-session-read-NIqS_5rS.mjs";
import { t as preparePersonalModelAccountSelection } from "./users-model-account-access-qMuKU1sR.mjs";
import { t as prepareAuthenticatedProfile } from "./users-profile-access-DMrWCwBO.mjs";
//#region src/gateway/server-methods/chat-metadata-handler.ts
/** Resolve saved-session grants or capture a new draft's current human authority. */
async function resolveChatMetadataReadParams(e, t, n) {
const { respond: r, context: i, client: a, signal: o } = e;
const s = i.getRuntimeConfig();
n?.assertCurrent();
const c = await prepareAuthenticatedProfile(e);
c.assertCurrent();
const l = readOperatorRolePolicyRevision();
const u = readUserProfileAliasRevision();
const d = a?.authenticatedUserProfile?.profileId;
const f = a?.authenticatedUserId;
const isRequestCurrent = () => !o?.aborted && readOperatorRolePolicyRevision() === l && readUserProfileAliasRevision() === u && a?.authenticatedUserProfile?.profileId === d && a?.authenticatedUserId === f;
const assertRequestCurrent = () => {
if (!isRequestCurrent() || i.getRuntimeConfig() !== s) throw new PreparedModelRuntimePublicationSupersededError("Chat metadata access changed while preparing its metadata. Retry the request.");
c.assertCurrent();
n?.assertCurrent();
};
if (t.sessionKey) {
const e = t.sessionKey;
const n = resolveRequestedSessionAgentId(s, t.sessionKey, normalizeOptionalString(t.agentId), "read");
if (!n.ok) {
r(false, void 0, n.error);
return;
}
const o = c.profileId;
const l = await prepareChatMetadataSessionRead({
cfg: s,
sessionKey: e,
agentId: n.agentId,
assertRequestCurrent
});
const u = l.selected;
let d;
const release = () => {
d?.release();
l.release();
};
const isCurrent = () => isRequestCurrent() && l.isCurrent();
const assertVisible = () => {
const t = (i.getCommittedRuntimeConfig ?? i.getRuntimeConfig)();
const n = createSessionListEntryFilter({
client: a,
cfg: t,
membership: d?.readCurrent(t).membership
});
if (u.entry && n?.(u.legacyKey ?? u.canonicalKey, u.entry) === false) throw new SessionMutationAuthorizationChangedError(hiddenSessionNotFound(e));
};
try {
if (operatorSessionCap(a, s) === "invited") d = await prepareSessionSharingRead({
cfg: s,
sessionKey: e,
agentId: n.agentId
});
assertVisible();
const r = u.entry?.sessionId;
const c = t.includeModels === false ? void 0 : await readSessionWorkerPlacementAsync({
context: i,
sessionId: r
});
const f = c ? readWorkerPlacementIdentity(c, i.workerEnvironmentService)?.inference : void 0;
assertVisible();
assertRequestCurrent();
l.assertCurrent();
return {
agentId: resolveSessionAgentId({
sessionKey: t.sessionKey,
config: u.cfg,
agentId: n.agentId
}),
sessionKey: u.canonicalKey,
storePath: u.readSource?.path ?? u.storePath,
sessionEntry: u.entry,
...f ? { workerInference: f } : {},
isCurrent,
assertCurrent: () => {
assertVisible();
assertRequestCurrent();
l.assertCurrent();
},
withCurrent: (e) => l.withCurrent(() => {
assertVisible();
return e();
}),
beforeRequest: () => {
assertVisible();
assertRequestCurrent();
l.beforeRequest();
},
release,
requesterProfileId: o
};
} catch (e) {
release();
throw e;
}
}
const p = resolveAgentIdOrRespondError({
rawAgentId: t.agentId?.trim() ? normalizeAgentId(t.agentId) : void 0,
respond: r,
cfg: s
});
if (!p) return;
assertRequestCurrent();
return {
agentId: p.agentId,
requesterProfileId: n?.owner ?? c.profileId,
isCurrent: isRequestCurrent,
assertCurrent: assertRequestCurrent,
...n ? { draftAccountSelection: n } : {}
};
}
const handleChatMetadataRequest = createPreparedReadHandler(async (e) => {
const { params: t, respond: n, context: r, client: i } = e;
if (!assertValidParams(t, validateChatMetadataParams, "chat.metadata", n)) return;
let a;
try {
a = await resolveChatMetadataReadParams(e, t, !t.sessionKey && t.authProfileId ? await preparePersonalModelAccountSelection(e, t.authProfileId, SESSION_READ_SCOPE) : void 0);
if (!a) return;
if (t.includeModels === false) {
a.includeModels = false;
a.ifRevision = t.ifRevision;
}
const n = a;
const assertCurrent = () => {
n.draftAccountSelection?.assertCurrent();
n.assertCurrent?.();
};
assertCurrent();
return {
assertCurrent,
release: n.release,
run: async (e) => {
const t = await r.readChatMetadata(n);
const publish = () => {
assertCurrent();
const a = r.getRuntimeConfig();
const o = t.models && prepareOperatorModelPresentation({
cfg: a,
policyConfig: r.getCommittedRuntimeConfig?.() ?? a,
client: i
})?.forAgent(n.agentId, t.models);
e(true, projectModelFastModeCatalog(o ? o.metadata(t) : t, i));
};
if (n.withCurrent) await n.withCurrent(publish);
else publish();
}
};
} catch (e) {
a?.release?.();
throw e;
}
}, (e, { respond: t }) => {
if (e instanceof ChatMetadataStartingError) {
t(false, void 0, errorShape(ErrorCodes.UNAVAILABLE, e.message, {
retryable: true,
retryAfterMs: 0
}));
return;
}
if (e instanceof SessionMutationAuthorizationChangedError) {
t(false, void 0, e.error);
return;
}
if (!(e instanceof ModelAccountConnectAuthorityError)) throw e;
t(false, void 0, errorShape(ErrorCodes.FORBIDDEN, e.message));
});
//#endregion
export { resolveChatMetadataReadParams as n, handleChatMetadataRequest as t };