openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
178 lines (177 loc) • 6.21 kB
JavaScript
import { l as normalizeOptionalString } from "./string-coerce-CCIiB4H7.mjs";
import { Ir as validateAuditActivityListParams, Lr as validateAuditListParams, Rr as validateAuditRunInspectParams } from "./src-BX8OAe1J.mjs";
import { t as ErrorCodes } from "./gateway-error-details-B69XiHdh.mjs";
import { p as errorShape } from "./error-codes-BzF7aRQ0.mjs";
import { f as findAuditActivityFilterConflict } from "./audit-activity-D5l1XT6-.mjs";
import { t as assertValidParams } from "./validation-DwNk6A-B.mjs";
import { a as isExecutionDecisionCursor, i as ExecutionDecisionCursorError, l as listAuditEvents, t as inspectExecutionIdentityRun } from "./execution-identity-context-BTEtnddC.mjs";
import { t as parsePositiveAuditCursor } from "./audit-cursor-f-Y7DpmR.mjs";
//#region src/gateway/server-methods/audit.ts
const DEFAULT_AUDIT_LIST_LIMIT = 100;
const MAX_AUDIT_LIST_LIMIT = 500;
function serializeAuditRunInspectResult(e) {
const t = {
schemaVersion: e.schemaVersion,
run: e.run,
identity: e.identity,
decisionDisplays: e.decisionDisplays,
coverage: e.coverage
};
if (e.nextDecisionCursor !== void 0) t.nextDecisionCursor = e.nextDecisionCursor;
if (e.nextExecutionCursor !== void 0) t.nextExecutionCursor = e.nextExecutionCursor;
return t;
}
function isOwnerDecisionCursor(e) {
return parsePositiveAuditCursor(e) === null && isExecutionDecisionCursor(e);
}
/** Preserve the shipped audit.list result shape for run/tool-only clients. */
function mapLegacyAuditEvent(e) {
if (e.kind === "message") throw new Error("legacy audit.list cannot project message records");
const { schemaVersion: t, actorType: n, actorId: r, ...i } = e;
return {
...i,
actor: {
type: n,
id: r
}
};
}
function mapAuditActivityEvent(e) {
if (e.kind === "agent_run") {
const { actorType: t, actorId: n, ...r } = e;
return {
...r,
eventType: "agent_run",
actor: {
type: t,
id: n
}
};
}
if (e.kind === "tool_action") {
const { actorType: t, actorId: n, ...r } = e;
return {
...r,
eventType: "tool_action",
actor: {
type: t,
id: n
}
};
}
if (e.direction === "inbound") {
const { actorType: t, actorId: n, ...r } = e;
const i = t === "channel_sender" ? {
type: "channel_sender",
id: n
} : {
type: "system",
id: n
};
return {
...r,
eventType: "inbound_message",
actor: i
};
}
if (e.action !== "message.outbound.finished") throw new Error("nonterminal outbound messages are not audit activity records");
const { actorType: t, actorId: n, ...r } = e;
return {
...r,
eventType: "outbound_message",
actor: {
type: t,
id: n
}
};
}
function invalidRangeOrCursor(e) {
const t = parsePositiveAuditCursor(e.cursor);
return {
...t !== void 0 && t !== null ? { cursor: t } : {},
invalid: t === null || e.after !== void 0 && e.before !== void 0 && e.after > e.before
};
}
function readAuditListPage(e, t, n = false) {
const r = normalizeOptionalString(e.agentId);
const i = normalizeOptionalString(e.sessionKey);
const a = normalizeOptionalString(e.runId);
return listAuditEvents({
limit: Math.min(e.limit ?? DEFAULT_AUDIT_LIST_LIMIT, MAX_AUDIT_LIST_LIMIT),
...t !== void 0 ? { cursor: t } : {},
filters: {
...n ? { includeMessages: true } : {},
...r ? { agentId: r } : {},
...i ? { sessionKey: i } : {},
...a ? { runId: a } : {},
...e.kind ? { kind: e.kind } : {},
...e.status ? { status: e.status } : {},
...n && e.direction ? { direction: e.direction } : {},
...n && e.channel ? { channel: e.channel } : {},
...e.after !== void 0 ? { after: e.after } : {},
...e.before !== void 0 ? { before: e.before } : {}
}
});
}
const auditHandlers = {
"audit.list": async ({ params: e, respond: t }) => {
if (!assertValidParams(e, validateAuditListParams, "audit.list", t)) return;
const n = invalidRangeOrCursor(e);
if (n.invalid) {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid audit.list range or cursor"));
return;
}
const r = await readAuditListPage(e, n.cursor);
t(true, {
events: r.events.map(mapLegacyAuditEvent),
...r.nextCursor !== void 0 ? { nextCursor: String(r.nextCursor) } : {}
});
},
"audit.activity.list": async ({ params: e, respond: t }) => {
if (!assertValidParams(e, validateAuditActivityListParams, "audit.activity.list", t)) return;
const n = findAuditActivityFilterConflict(e);
if (n) {
const e = n.type === "kind" ? `${n.field} only applies to kind ${n.supportedKinds.join(" or ")}` : `${n.field} cannot be combined with ${n.conflictingField}`;
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, `invalid audit.activity.list filters: ${e}`));
return;
}
const r = invalidRangeOrCursor(e);
if (r.invalid) {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid audit.activity.list range or cursor"));
return;
}
const i = await readAuditListPage(e, r.cursor, true);
t(true, {
events: i.events.map(mapAuditActivityEvent),
...i.nextCursor !== void 0 ? { nextCursor: String(i.nextCursor) } : {}
});
},
"audit.run.inspect": async ({ params: e, respond: t }) => {
if (!assertValidParams(e, validateAuditRunInspectParams, "audit.run.inspect", t)) return;
const n = e.decisionCursor;
const r = typeof e.runId !== "string" || e.executionCursor === n && n !== void 0 && isOwnerDecisionCursor(n) ? void 0 : parsePositiveAuditCursor(e.executionCursor);
if (n !== void 0 && !isExecutionDecisionCursor(n) || r === null) {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, "invalid audit.run.inspect cursor"));
return;
}
try {
t(true, serializeAuditRunInspectResult(await inspectExecutionIdentityRun({
...typeof e.runId === "string" ? {
runId: e.runId,
...r !== void 0 ? { executionOffset: r } : {},
executionLimit: e.executionLimit ?? 50
} : { executionId: e.executionId },
...n !== void 0 ? { decisionCursor: n } : {},
decisionLimit: e.decisionLimit ?? 50
})));
} catch (e) {
if (e instanceof ExecutionDecisionCursorError) {
t(false, void 0, errorShape(ErrorCodes.INVALID_REQUEST, e.message));
return;
}
throw e;
}
}
};
//#endregion
export { auditHandlers };