UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

361 lines (360 loc) • 11.5 kB
import { t as closedObject } from "./closed-object-B-B1FwhS.mjs"; import { t as stringLiteralUnion } from "./string-literal-union-C2qFTHSZ.mjs"; import { a as NonEmptyString } from "./primitives-C8sb4NEJ.mjs"; import { Type } from "typebox"; //#region packages/gateway-protocol/src/schema/audit-activity.ts const AuditActivitySchemaVersionV1Schema = Type.Integer({ minimum: 1, maximum: 1 }); const AUDIT_ACTIVITY_STATUSES = [ "started", "succeeded", "failed", "cancelled", "timed_out", "blocked", "unknown" ]; const AUDIT_ACTIVITY_MESSAGE_KIND = "message"; const AUDIT_ACTIVITY_KINDS = [ "agent_run", "tool_action", AUDIT_ACTIVITY_MESSAGE_KIND ]; const AUDIT_ACTIVITY_DIRECTIONS = ["inbound", "outbound"]; const AUDIT_ACTIVITY_NON_MESSAGE_KINDS = ["agent_run", "tool_action"]; function findAuditActivityFilterConflict(e) { const t = e.direction !== void 0 ? "direction" : e.channel !== void 0 ? "channel" : void 0; const n = e.sessionKey !== void 0; if (e.kind === "message" && n) return { type: "kind", field: "sessionKey", supportedKinds: AUDIT_ACTIVITY_NON_MESSAGE_KINDS }; if (e.kind !== void 0 && e.kind !== "message" && t) return { type: "kind", field: t, supportedKinds: [AUDIT_ACTIVITY_MESSAGE_KIND] }; if (e.kind === void 0 && n && t) return { type: "filter", field: t, conflictingField: "sessionKey" }; } const AuditActivityStatusV1Schema = Type.Union(AUDIT_ACTIVITY_STATUSES.map((e) => Type.Literal(e))); const AuditActivityKindV1Schema = Type.Union(AUDIT_ACTIVITY_KINDS.map((e) => Type.Literal(e))); const AuditActivityDirectionV1Schema = Type.Union(AUDIT_ACTIVITY_DIRECTIONS.map((e) => Type.Literal(e))); const AuditActivityConversationKindV1Schema = stringLiteralUnion([ "direct", "group", "channel", "unknown" ]); const AuditActivityHmacRefV1Schema = Type.String({ pattern: "^hmac-sha256:v1:[a-f0-9]{32}:[a-f0-9]{64}$" }); const AuditActivityAgentActorV1Schema = closedObject({ type: stringLiteralUnion(["agent", "system"]), id: NonEmptyString }); const AuditActivityInboundActorV1Schema = Type.Union([closedObject({ type: Type.Literal("channel_sender"), id: AuditActivityHmacRefV1Schema }), closedObject({ type: Type.Literal("system"), id: NonEmptyString })]); const AuditActivityOutboundActorV1Schema = closedObject({ type: stringLiteralUnion(["agent", "system"]), id: NonEmptyString }); const commonProperties = { schemaVersion: AuditActivitySchemaVersionV1Schema, eventId: NonEmptyString, sequence: Type.Integer({ minimum: 1 }), sourceSequence: Type.Integer({ minimum: 1 }), occurredAt: Type.Integer({ minimum: 0 }), redaction: Type.Literal("metadata_only") }; const agentProperties = { actor: AuditActivityAgentActorV1Schema, agentId: NonEmptyString, sessionKey: Type.Optional(NonEmptyString), sessionId: Type.Optional(NonEmptyString), runId: NonEmptyString }; const messageProperties = { channel: NonEmptyString, conversationKind: AuditActivityConversationKindV1Schema, durationMs: Type.Optional(Type.Integer({ minimum: 0 })), resultCount: Type.Optional(Type.Integer({ minimum: 0 })), agentId: Type.Optional(NonEmptyString), runId: Type.Optional(NonEmptyString), accountRef: Type.Optional(AuditActivityHmacRefV1Schema), conversationRef: Type.Optional(AuditActivityHmacRefV1Schema), messageRef: Type.Optional(AuditActivityHmacRefV1Schema), targetRef: Type.Optional(AuditActivityHmacRefV1Schema) }; function correlatedObject(e, t) { return Type.Object(e, { additionalProperties: false, allOf: [t] }); } function withoutField(e) { return { not: { required: [e] } }; } const withoutErrorCode = withoutField("errorCode"); const withoutReasonCode = withoutField("reasonCode"); const withoutFailureStage = withoutField("failureStage"); const withoutDeliveryKind = withoutField("deliveryKind"); function actionTerminalVariants(e, t) { return Type.Union([ Type.Intersect([Type.Object({ action: Type.Literal(`${e}.started`), status: Type.Literal("started") }), withoutErrorCode]), Type.Intersect([Type.Object({ action: Type.Literal(`${e}.finished`), status: Type.Literal("succeeded") }), withoutErrorCode]), ...t.map(([t, n]) => Type.Object({ action: Type.Literal(`${e}.finished`), status: Type.Literal(t), errorCode: Type.Literal(n) })) ]); } const agentRunProperties = { eventType: Type.Literal("agent_run"), ...commonProperties, ...agentProperties, kind: Type.Literal("agent_run") }; /** V1 agent-run activity record. */ const AuditActivityAgentRunV1Schema = correlatedObject({ ...agentRunProperties, action: stringLiteralUnion(["agent.run.started", "agent.run.finished"]), status: stringLiteralUnion([ "started", "succeeded", "failed", "cancelled", "timed_out", "blocked" ]), errorCode: Type.Optional(stringLiteralUnion([ "run_failed", "run_cancelled", "run_timed_out", "run_blocked" ])) }, actionTerminalVariants("agent.run", [ ["failed", "run_failed"], ["cancelled", "run_cancelled"], ["timed_out", "run_timed_out"], ["blocked", "run_blocked"] ])); const toolActionProperties = { eventType: Type.Literal("tool_action"), ...commonProperties, ...agentProperties, kind: Type.Literal("tool_action"), toolCallId: Type.Optional(NonEmptyString), toolName: Type.Optional(NonEmptyString) }; /** V1 tool-action activity record. */ const AuditActivityToolActionV1Schema = correlatedObject({ ...toolActionProperties, action: stringLiteralUnion(["tool.action.started", "tool.action.finished"]), status: AuditActivityStatusV1Schema, errorCode: Type.Optional(stringLiteralUnion([ "tool_failed", "tool_cancelled", "tool_timed_out", "tool_blocked", "tool_outcome_unknown" ])) }, actionTerminalVariants("tool.action", [ ["failed", "tool_failed"], ["cancelled", "tool_cancelled"], ["timed_out", "tool_timed_out"], ["blocked", "tool_blocked"], ["unknown", "tool_outcome_unknown"] ])); const inboundMessageProperties = { eventType: Type.Literal("inbound_message"), ...commonProperties, ...messageProperties, kind: Type.Literal("message"), action: Type.Literal("message.inbound.processed"), direction: Type.Literal("inbound"), actor: AuditActivityInboundActorV1Schema }; const inboundCompletedReasonSchema = stringLiteralUnion([ "fast_abort", "plugin_bound_handled", "plugin_bound_unavailable", "plugin_bound_declined", "before_dispatch_handled", "acp_dispatch_completed", "acp_dispatch_empty", "active_run_injected" ]); const inboundSkippedReasonSchema = stringLiteralUnion([ "duplicate", "reply_operation_active", "reply_operation_aborted", "acp_dispatch_aborted" ]); /** V1 inbound-message activity record. */ const inboundFailureReasonSchema = stringLiteralUnion(["acp_dispatch_failed", "plugin_bound_error"]); const AuditActivityInboundMessageV1Schema = correlatedObject({ ...inboundMessageProperties, status: stringLiteralUnion([ "succeeded", "blocked", "failed" ]), outcome: stringLiteralUnion([ "completed", "skipped", "failed" ]), errorCode: Type.Optional(Type.Literal("message_processing_failed")), reasonCode: Type.Optional(Type.Union([ ...inboundCompletedReasonSchema.anyOf, ...inboundSkippedReasonSchema.anyOf, ...inboundFailureReasonSchema.anyOf ])) }, Type.Union([ Type.Intersect([Type.Object({ status: Type.Literal("succeeded"), outcome: Type.Literal("completed"), reasonCode: Type.Optional(inboundCompletedReasonSchema) }), withoutErrorCode]), Type.Intersect([Type.Object({ status: Type.Literal("blocked"), outcome: Type.Literal("skipped"), reasonCode: Type.Optional(inboundSkippedReasonSchema) }), withoutErrorCode]), Type.Object({ status: Type.Literal("failed"), outcome: Type.Literal("failed"), errorCode: Type.Literal("message_processing_failed"), reasonCode: Type.Optional(inboundFailureReasonSchema) }) ])); const outboundMessageProperties = { eventType: Type.Literal("outbound_message"), ...commonProperties, ...messageProperties, kind: Type.Literal("message"), action: Type.Literal("message.outbound.finished"), direction: Type.Literal("outbound"), actor: AuditActivityOutboundActorV1Schema, deliveryKind: Type.Optional(stringLiteralUnion([ "text", "media", "other" ])) }; const outboundSuppressedReasonSchema = stringLiteralUnion([ "cancelled_by_message_sending_hook", "cancelled_by_reply_payload_sending_hook", "empty_after_message_sending_hook", "empty_after_reply_payload_sending_hook", "no_visible_payload" ]); const outboundFailureStageSchema = stringLiteralUnion([ "platform_send", "queue", "unknown" ]); /** V1 outbound-message activity record. */ const outboundFailureErrorSchema = stringLiteralUnion(["message_delivery_failed", "message_delivery_partial_failure"]); const AuditActivityOutboundMessageV1Schema = correlatedObject({ ...outboundMessageProperties, status: stringLiteralUnion([ "succeeded", "blocked", "failed", "unknown" ]), outcome: stringLiteralUnion([ "sent", "suppressed", "failed", "unknown" ]), errorCode: Type.Optional(outboundFailureErrorSchema), reasonCode: Type.Optional(outboundSuppressedReasonSchema), failureStage: Type.Optional(outboundFailureStageSchema) }, Type.Union([ Type.Intersect([ Type.Object({ status: Type.Literal("succeeded"), outcome: Type.Literal("sent") }), withoutErrorCode, withoutReasonCode, withoutFailureStage ]), Type.Intersect([ Type.Object({ status: Type.Literal("blocked"), outcome: Type.Literal("suppressed"), reasonCode: outboundSuppressedReasonSchema }), withoutErrorCode, withoutFailureStage, withoutDeliveryKind ]), Type.Intersect([Type.Object({ status: Type.Literal("failed"), outcome: Type.Literal("failed"), errorCode: outboundFailureErrorSchema, failureStage: outboundFailureStageSchema }), withoutReasonCode]), Type.Intersect([ Type.Object({ status: Type.Literal("unknown"), outcome: Type.Literal("unknown"), failureStage: outboundFailureStageSchema }), withoutErrorCode, withoutReasonCode, withoutDeliveryKind ]) ])); /** Discriminated V1 activity record union. */ const AuditActivityEventV1Schema = Type.Union([ AuditActivityAgentRunV1Schema, AuditActivityToolActionV1Schema, AuditActivityInboundMessageV1Schema, AuditActivityOutboundMessageV1Schema ]); /** Bounded newest-first V1 activity query filters. */ const AuditActivityListParamsSchema = closedObject({ agentId: Type.Optional(NonEmptyString), sessionKey: Type.Optional(NonEmptyString), runId: Type.Optional(NonEmptyString), kind: Type.Optional(AuditActivityKindV1Schema), status: Type.Optional(AuditActivityStatusV1Schema), direction: Type.Optional(AuditActivityDirectionV1Schema), channel: Type.Optional(NonEmptyString), after: Type.Optional(Type.Integer({ minimum: 0 })), before: Type.Optional(Type.Integer({ minimum: 0 })), limit: Type.Optional(Type.Integer({ minimum: 1, maximum: 500 })), cursor: Type.Optional(NonEmptyString) }); /** Stable sequence-cursor V1 activity page. */ const AuditActivityListResultSchema = closedObject({ events: Type.Array(AuditActivityEventV1Schema), nextCursor: Type.Optional(NonEmptyString) }); //#endregion export { AuditActivityAgentRunV1Schema as a, AuditActivityListParamsSchema as c, AuditActivityToolActionV1Schema as d, findAuditActivityFilterConflict as f, AUDIT_ACTIVITY_STATUSES as i, AuditActivityListResultSchema as l, AUDIT_ACTIVITY_KINDS as n, AuditActivityEventV1Schema as o, AUDIT_ACTIVITY_MESSAGE_KIND as r, AuditActivityInboundMessageV1Schema as s, AUDIT_ACTIVITY_DIRECTIONS as t, AuditActivityOutboundMessageV1Schema as u };