fix(memory): respect memory slot in dreaming config

This commit is contained in:
sky
2026-04-07 11:36:24 +08:00
committed by Vignesh
parent 24d4acb274
commit 9dda94c0f7
6 changed files with 210 additions and 12 deletions

View File

@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import {
loadDreamDiary,
loadDreamingStatus,
resolveConfiguredDreaming,
updateDreamingEnabled,
type DreamingState,
} from "./dreaming.ts";
@@ -105,6 +106,25 @@ describe("dreaming controller", () => {
it("patches config to update global dreaming enablement", async () => {
const { state, request } = createState();
state.configSnapshot = {
hash: "hash-1",
config: {
plugins: {
slots: {
memory: "memos-local-openclaw-plugin",
},
entries: {
"memos-local-openclaw-plugin": {
config: {
dreaming: {
enabled: true,
},
},
},
},
},
},
};
request.mockResolvedValue({ ok: true });
const ok = await updateDreamingEnabled(state, false);
@@ -117,10 +137,55 @@ describe("dreaming controller", () => {
sessionKey: "main",
}),
);
const requestPayload = request.mock.calls[0]?.[1] as { raw?: string };
expect(JSON.parse(String(requestPayload.raw))).toEqual({
plugins: {
entries: {
"memos-local-openclaw-plugin": {
config: {
dreaming: {
enabled: false,
},
},
},
},
},
});
expect(state.dreamingModeSaving).toBe(false);
expect(state.dreamingStatusError).toBeNull();
});
it("reads dreaming enabled state from the selected memory slot plugin", () => {
expect(
resolveConfiguredDreaming({
plugins: {
slots: {
memory: "memos-local-openclaw-plugin",
},
entries: {
"memos-local-openclaw-plugin": {
config: {
dreaming: {
enabled: true,
},
},
},
"memory-core": {
config: {
dreaming: {
enabled: false,
},
},
},
},
},
}),
).toEqual({
pluginId: "memos-local-openclaw-plugin",
enabled: true,
});
});
it("fails gracefully when config hash is missing", async () => {
const { state, request } = createState();
state.configSnapshot = {};

View File

@@ -3,6 +3,7 @@ import type { ConfigSnapshot } from "../types.ts";
export type DreamingPhaseId = "light" | "deep" | "rem";
const DEFAULT_DREAM_DIARY_PATH = "DREAMS.md";
const DEFAULT_DREAMING_PLUGIN_ID = "memory-core";
type DreamingPhaseStatusBase = {
enabled: boolean;
@@ -139,6 +140,32 @@ function normalizePhaseStatusBase(record: Record<string, unknown> | null): Dream
};
}
function resolveDreamingPluginId(configValue: Record<string, unknown> | null): string {
const plugins = asRecord(configValue?.plugins);
const slots = asRecord(plugins?.slots);
const configuredSlot = normalizeTrimmedString(slots?.memory);
if (configuredSlot && configuredSlot.toLowerCase() !== "none") {
return configuredSlot;
}
return DEFAULT_DREAMING_PLUGIN_ID;
}
export function resolveConfiguredDreaming(configValue: Record<string, unknown> | null): {
pluginId: string;
enabled: boolean;
} {
const pluginId = resolveDreamingPluginId(configValue);
const plugins = asRecord(configValue?.plugins);
const entries = asRecord(plugins?.entries);
const pluginEntry = asRecord(entries?.[pluginId]);
const config = asRecord(pluginEntry?.config);
const dreaming = asRecord(config?.dreaming);
return {
pluginId,
enabled: normalizeBoolean(dreaming?.enabled, false),
};
}
function normalizeDreamingStatus(raw: unknown): DreamingStatus | null {
const record = asRecord(raw);
if (!record) {
@@ -286,10 +313,11 @@ export async function updateDreamingEnabled(
state: DreamingState,
enabled: boolean,
): Promise<boolean> {
const { pluginId } = resolveConfiguredDreaming(asRecord(state.configSnapshot?.config) ?? null);
const ok = await writeDreamingPatch(state, {
plugins: {
entries: {
"memory-core": {
[pluginId]: {
config: {
dreaming: {
enabled,