fix(types): Add EventStoreConfig type + fix NATS enum imports

This commit is contained in:
Claudia 2026-02-02 09:35:50 +01:00
parent 77dbafc77d
commit ba9f76c027
3 changed files with 12702 additions and 14 deletions

12668
package-lock.json generated Normal file

File diff suppressed because it is too large Load diff

View file

@ -207,6 +207,17 @@ export type GatewayNodesConfig = {
denyCommands?: string[];
};
export type EventStoreConfig = {
/** Enable Event Store (NATS JetStream) integration. */
enabled?: boolean;
/** NATS server URL (default: nats://localhost:4222). */
natsUrl?: string;
/** JetStream stream name (default: openclaw-events). */
streamName?: string;
/** Subject prefix for events (default: openclaw.events). */
subjectPrefix?: string;
};
export type GatewayConfig = {
/** Single multiplexed port for Gateway WS + HTTP (default: 18789). */
port?: number;
@ -235,6 +246,8 @@ export type GatewayConfig = {
tls?: GatewayTlsConfig;
http?: GatewayHttpConfig;
nodes?: GatewayNodesConfig;
/** Event Store (NATS JetStream) configuration for persistent event logging. */
eventStore?: EventStoreConfig;
/**
* IPs of trusted reverse proxies (e.g. Traefik, nginx). When a connection
* arrives from one of these IPs, the Gateway trusts `x-forwarded-for` (or

View file

@ -1,6 +1,6 @@
/**
* Event Store Integration for OpenClaw
*
*
* Publishes all agent events to NATS JetStream for persistent storage.
* This enables:
* - Full audit trail of all interactions
@ -9,7 +9,14 @@
* - Time-travel debugging
*/
import { connect, type NatsConnection, type JetStreamClient, StringCodec } from "nats";
import {
connect,
type NatsConnection,
type JetStreamClient,
StringCodec,
RetentionPolicy,
StorageType,
} from "nats";
import type { AgentEventPayload } from "./agent-events.js";
import { onAgentEvent } from "./agent-events.js";
@ -37,7 +44,7 @@ export type ClawEvent = {
};
};
export type EventType =
export type EventType =
| "conversation.message.in"
| "conversation.message.out"
| "conversation.tool_call"
@ -129,7 +136,7 @@ async function publishEvent(evt: AgentEventPayload): Promise<void> {
const clawEvent = toClawEvent(evt);
const subject = `${eventStoreConfig.subjectPrefix}.${clawEvent.agent}.${clawEvent.type.replace(/\./g, "_")}`;
const payload = sc.encode(JSON.stringify(clawEvent));
await jetstream.publish(subject, payload);
} catch (err) {
// Log but don't throw — event store should never break core functionality
@ -142,7 +149,7 @@ async function publishEvent(evt: AgentEventPayload): Promise<void> {
*/
async function ensureStream(js: JetStreamClient, config: EventStoreConfig): Promise<void> {
const jsm = await natsConnection!.jetstreamManager();
try {
await jsm.streams.info(config.streamName);
} catch {
@ -150,11 +157,11 @@ async function ensureStream(js: JetStreamClient, config: EventStoreConfig): Prom
await jsm.streams.add({
name: config.streamName,
subjects: [`${config.subjectPrefix}.>`],
retention: "limits" as const,
retention: RetentionPolicy.Limits,
max_msgs: -1,
max_bytes: -1,
max_age: 0, // Never expire
storage: "file" as const,
storage: StorageType.File,
num_replicas: 1,
duplicate_window: 120_000_000_000, // 2 minutes in nanoseconds
});
@ -173,23 +180,23 @@ export async function initEventStore(config: EventStoreConfig): Promise<void> {
try {
eventStoreConfig = config;
// Connect to NATS
natsConnection = await connect({ servers: config.natsUrl });
console.log(`[event-store] Connected to NATS at ${config.natsUrl}`);
// Get JetStream client
jetstream = natsConnection.jetstream();
// Ensure stream exists
await ensureStream(jetstream, config);
// Subscribe to all agent events
unsubscribe = onAgentEvent((evt) => {
// Fire and forget — don't await to avoid blocking the event loop
publishEvent(evt).catch(() => {});
});
console.log("[event-store] Event listener registered");
} catch (err) {
console.error("[event-store] Failed to initialize:", err);
@ -205,13 +212,13 @@ export async function shutdownEventStore(): Promise<void> {
unsubscribe();
unsubscribe = null;
}
if (natsConnection) {
await natsConnection.drain();
natsConnection = null;
jetstream = null;
}
eventStoreConfig = null;
console.log("[event-store] Shutdown complete");
}