feat(core): Auto-load Event Context on session start (Phase 3)
Some checks are pending
CI / install-check (push) Waiting to run
CI / checks (bunx tsc -p tsconfig.json --noEmit false, bun, build) (push) Waiting to run
CI / checks (pnpm build && pnpm lint, node, lint) (push) Waiting to run
CI / checks (pnpm canvas:a2ui:bundle && bunx vitest run, bun, test) (push) Waiting to run
CI / checks (pnpm canvas:a2ui:bundle && pnpm test, node, test) (push) Waiting to run
CI / checks (pnpm format, node, format) (push) Waiting to run
CI / checks (pnpm protocol:check, node, protocol) (push) Waiting to run
CI / checks (pnpm tsgo, node, tsgo) (push) Waiting to run
CI / secrets (push) Waiting to run
CI / checks-windows (pnpm build && pnpm lint, node, build & lint) (push) Waiting to run
CI / checks-windows (pnpm canvas:a2ui:bundle && pnpm test, node, test) (push) Waiting to run
CI / checks-windows (pnpm protocol:check, node, protocol) (push) Waiting to run
CI / checks-macos (pnpm test, test) (push) Waiting to run
CI / macos-app (set -euo pipefail for attempt in 1 2 3; do if swift build --package-path apps/macos --configuration release; then exit 0 fi echo "swift build failed (attempt $attempt/3). Retrying…" sleep $((attempt * 20)) done exit 1 , build) (push) Waiting to run
CI / macos-app (set -euo pipefail for attempt in 1 2 3; do if swift test --package-path apps/macos --parallel --enable-code-coverage --show-codecov-path; then exit 0 fi echo "swift test failed (attempt $attempt/3). Retrying…" sleep $((attempt … (push) Waiting to run
CI / macos-app (swiftlint --config .swiftlint.yml swiftformat --lint apps/macos/Sources --config .swiftformat , lint) (push) Waiting to run
CI / ios (push) Waiting to run
CI / android (./gradlew --no-daemon :app:assembleDebug, build) (push) Waiting to run
CI / android (./gradlew --no-daemon :app:testDebugUnitTest, test) (push) Waiting to run
Workflow Sanity / no-tabs (push) Waiting to run

- Load event context from NATS JetStream on every run
- Inject formatted context into system prompt
- Configurable via gateway.eventStore settings
- Graceful fallback if NATS unavailable

Now every session starts with recent event history!
This commit is contained in:
Claudia 2026-02-02 10:52:56 +01:00
parent 6597fccd42
commit 1fc8ba54f1
2 changed files with 32 additions and 0 deletions

View file

@ -7,6 +7,7 @@ import os from "node:os";
import type { EmbeddedRunAttemptParams, EmbeddedRunAttemptResult } from "./types.js"; import type { EmbeddedRunAttemptParams, EmbeddedRunAttemptResult } from "./types.js";
import { resolveHeartbeatPrompt } from "../../../auto-reply/heartbeat.js"; import { resolveHeartbeatPrompt } from "../../../auto-reply/heartbeat.js";
import { resolveChannelCapabilities } from "../../../config/channel-capabilities.js"; import { resolveChannelCapabilities } from "../../../config/channel-capabilities.js";
import { buildEventContext, formatContextForPrompt } from "../../../infra/event-context.js";
import { getMachineDisplayName } from "../../../infra/machine-name.js"; import { getMachineDisplayName } from "../../../infra/machine-name.js";
import { MAX_IMAGE_BYTES } from "../../../media/constants.js"; import { MAX_IMAGE_BYTES } from "../../../media/constants.js";
import { getGlobalHookRunner } from "../../../plugins/hook-runner-global.js"; import { getGlobalHookRunner } from "../../../plugins/hook-runner-global.js";
@ -341,6 +342,33 @@ export async function runEmbeddedAttempt(
}); });
const ttsHint = params.config ? buildTtsSystemPromptHint(params.config) : undefined; const ttsHint = params.config ? buildTtsSystemPromptHint(params.config) : undefined;
// Load event-sourced context if enabled
let eventContextHint: string | undefined;
const eventStoreConfig = params.config?.gateway?.eventStore;
if (eventStoreConfig?.enabled) {
try {
const eventContext = await buildEventContext(
{
natsUrl: eventStoreConfig.natsUrl || "nats://localhost:4222",
streamName: eventStoreConfig.streamName || "openclaw-events",
subjectPrefix: eventStoreConfig.subjectPrefix || "openclaw.events",
},
{
agent: "agent",
sessionKey: params.sessionKey,
hoursBack: 2,
maxEvents: 100,
},
);
if (eventContext.eventsProcessed > 0) {
eventContextHint = formatContextForPrompt(eventContext);
log.info(`[event-context] Loaded ${eventContext.eventsProcessed} events for context`);
}
} catch (err) {
log.warn(`[event-context] Failed to load: ${err}`);
}
}
const appendPrompt = buildEmbeddedSystemPrompt({ const appendPrompt = buildEmbeddedSystemPrompt({
workspaceDir: effectiveWorkspace, workspaceDir: effectiveWorkspace,
defaultThinkLevel: params.thinkLevel, defaultThinkLevel: params.thinkLevel,
@ -366,6 +394,7 @@ export async function runEmbeddedAttempt(
userTime, userTime,
userTimeFormat, userTimeFormat,
contextFiles, contextFiles,
eventContextHint,
}); });
const systemPromptReport = buildSystemPromptReport({ const systemPromptReport = buildSystemPromptReport({
source: "run", source: "run",

View file

@ -46,6 +46,8 @@ export function buildEmbeddedSystemPrompt(params: {
userTime?: string; userTime?: string;
userTimeFormat?: ResolvedTimeFormat; userTimeFormat?: ResolvedTimeFormat;
contextFiles?: EmbeddedContextFile[]; contextFiles?: EmbeddedContextFile[];
/** Event-sourced context from NATS (formatted text block). */
eventContextHint?: string;
}): string { }): string {
return buildAgentSystemPrompt({ return buildAgentSystemPrompt({
workspaceDir: params.workspaceDir, workspaceDir: params.workspaceDir,
@ -71,6 +73,7 @@ export function buildEmbeddedSystemPrompt(params: {
userTime: params.userTime, userTime: params.userTime,
userTimeFormat: params.userTimeFormat, userTimeFormat: params.userTimeFormat,
contextFiles: params.contextFiles, contextFiles: params.contextFiles,
eventContextHint: params.eventContextHint,
}); });
} }