Skip to content

Commit 2fe39ce

Browse files
committed
refactor: rebase runtime config writes
1 parent fb3aa15 commit 2fe39ce

19 files changed

Lines changed: 373 additions & 227 deletions

File tree

‎extensions/active-memory/index.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2957,19 +2957,23 @@ export default definePluginEntry({
29572957
};
29582958
}
29592959
if (action === "on" || action === "enable" || action === "enabled") {
2960-
const nextConfig = updateActiveMemoryGlobalEnabledInConfig(currentConfig, true);
2961-
await api.runtime.config.replaceConfigFile({
2962-
nextConfig,
2960+
await api.runtime.config.mutateConfigFile({
29632961
afterWrite: { mode: "auto" },
2962+
mutate: (draft) => {
2963+
const nextConfig = updateActiveMemoryGlobalEnabledInConfig(draft, true);
2964+
Object.assign(draft, nextConfig);
2965+
},
29642966
});
29652967
refreshLiveConfigFromRuntime();
29662968
return { text: "Active Memory: on globally." };
29672969
}
29682970
if (action === "off" || action === "disable" || action === "disabled") {
2969-
const nextConfig = updateActiveMemoryGlobalEnabledInConfig(currentConfig, false);
2970-
await api.runtime.config.replaceConfigFile({
2971-
nextConfig,
2971+
await api.runtime.config.mutateConfigFile({
29722972
afterWrite: { mode: "auto" },
2973+
mutate: (draft) => {
2974+
const nextConfig = updateActiveMemoryGlobalEnabledInConfig(draft, false);
2975+
Object.assign(draft, nextConfig);
2976+
},
29732977
});
29742978
refreshLiveConfigFromRuntime();
29752979
return { text: "Active Memory: off globally." };

‎extensions/browser/src/browser/control-auth.ts‎

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
normalizeLowercaseStringOrEmpty,
44
normalizeOptionalString,
55
} from "openclaw/plugin-sdk/string-coerce-runtime";
6-
import { getRuntimeConfig, replaceConfigFile } from "../config/config.js";
6+
import { getRuntimeConfig, mutateConfigFile } from "../config/config.js";
77
import type { OpenClawConfig } from "../config/config.js";
88
import { resolveGatewayAuth } from "../gateway/auth.js";
99
import { ensureGatewayStartupAuth } from "../gateway/startup-auth.js";
@@ -77,19 +77,17 @@ async function generateAndPersistBrowserControlToken(params: {
7777
generatedToken?: string;
7878
}> {
7979
const token = generateBrowserControlToken();
80-
const nextCfg: OpenClawConfig = {
81-
...params.cfg,
82-
gateway: {
83-
...params.cfg.gateway,
84-
auth: {
85-
...params.cfg.gateway?.auth,
86-
token,
87-
},
88-
},
89-
};
90-
await replaceConfigFile({
91-
nextConfig: nextCfg,
80+
await mutateConfigFile({
9281
afterWrite: { mode: "auto" },
82+
mutate: (draft) => {
83+
draft.gateway = {
84+
...draft.gateway,
85+
auth: {
86+
...draft.gateway?.auth,
87+
token,
88+
},
89+
};
90+
},
9391
});
9492

9593
// Re-read to stay consistent with any concurrent config writer.
@@ -112,19 +110,17 @@ async function generateAndPersistBrowserControlPassword(params: {
112110
generatedToken?: string;
113111
}> {
114112
const password = generateBrowserControlToken();
115-
const nextCfg: OpenClawConfig = {
116-
...params.cfg,
117-
gateway: {
118-
...params.cfg.gateway,
119-
auth: {
120-
...params.cfg.gateway?.auth,
121-
password,
122-
},
123-
},
124-
};
125-
await replaceConfigFile({
126-
nextConfig: nextCfg,
113+
await mutateConfigFile({
127114
afterWrite: { mode: "auto" },
115+
mutate: (draft) => {
116+
draft.gateway = {
117+
...draft.gateway,
118+
auth: {
119+
...draft.gateway?.auth,
120+
password,
121+
},
122+
};
123+
},
128124
});
129125

130126
// Re-read to stay consistent with any concurrent config writer.

‎extensions/browser/src/browser/profiles-service.ts‎

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from "node:fs";
22
import path from "node:path";
33
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
44
import type { BrowserProfileConfig, OpenClawConfig } from "../config/config.js";
5-
import { getRuntimeConfig, replaceConfigFile } from "../config/config.js";
5+
import { getRuntimeConfig, mutateConfigFile } from "../config/config.js";
66
import { deriveDefaultBrowserCdpPortRange } from "../config/port-defaults.js";
77
import { formatErrorMessage } from "../infra/errors.js";
88
import { resolveUserPath } from "../utils.js";
@@ -165,20 +165,17 @@ export function createBrowserProfilesService(ctx: BrowserRouteContext) {
165165
}
166166
}
167167

168-
const nextConfig: OpenClawConfig = {
169-
...cfg,
170-
browser: {
171-
...cfg.browser,
172-
profiles: {
173-
...rawProfiles,
174-
[name]: profileConfig,
175-
},
176-
},
177-
};
178-
179-
await replaceConfigFile({
180-
nextConfig,
168+
await mutateConfigFile({
181169
afterWrite: { mode: "auto" },
170+
mutate: (draft) => {
171+
draft.browser = {
172+
...draft.browser,
173+
profiles: {
174+
...(draft.browser?.profiles ?? {}),
175+
[name]: profileConfig,
176+
},
177+
};
178+
},
182179
});
183180

184181
state.resolved.profiles[name] = profileConfig;
@@ -240,18 +237,15 @@ export function createBrowserProfilesService(ctx: BrowserRouteContext) {
240237
}
241238
}
242239

243-
const { [name]: _removed, ...remainingProfiles } = profiles;
244-
const nextConfig: OpenClawConfig = {
245-
...cfg,
246-
browser: {
247-
...cfg.browser,
248-
profiles: remainingProfiles,
249-
},
250-
};
251-
252-
await replaceConfigFile({
253-
nextConfig,
240+
await mutateConfigFile({
254241
afterWrite: { mode: "auto" },
242+
mutate: (draft) => {
243+
const { [name]: _removed, ...remainingProfiles } = draft.browser?.profiles ?? {};
244+
draft.browser = {
245+
...draft.browser,
246+
profiles: remainingProfiles,
247+
};
248+
},
255249
});
256250

257251
delete state.resolved.profiles[name];

‎extensions/browser/src/config/config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export {
22
getRuntimeConfig,
33
getRuntimeConfigSnapshot,
44
getRuntimeConfigSourceSnapshot,
5+
mutateConfigFile,
56
replaceConfigFile,
67
type BrowserConfig,
78
type BrowserProfileConfig,

‎extensions/browser/src/sdk-config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export {
55
getRuntimeConfigSnapshot,
66
getRuntimeConfigSourceSnapshot,
77
} from "openclaw/plugin-sdk/runtime-config-snapshot";
8-
export { replaceConfigFile } from "openclaw/plugin-sdk/config-mutation";
8+
export { mutateConfigFile, replaceConfigFile } from "openclaw/plugin-sdk/config-mutation";
99
export {
1010
type BrowserConfig,
1111
type BrowserProfileConfig,

‎extensions/memory-core/src/dreaming-command.ts‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,19 @@ export async function handleDreamingCommand(api: OpenClawPluginApi, ctx: PluginC
101101
return { text: "⚠️ /dreaming on|off requires operator.admin for gateway clients." };
102102
}
103103
const enabled = firstToken === "on";
104-
const nextConfig = updateDreamingEnabledInConfig(currentConfig, enabled);
105-
await api.runtime.config.replaceConfigFile({
106-
nextConfig,
104+
const committed = await api.runtime.config.mutateConfigFile({
107105
afterWrite: { mode: "auto" },
106+
mutate: (draft) => {
107+
const nextConfig = updateDreamingEnabledInConfig(draft, enabled);
108+
Object.assign(draft, nextConfig);
109+
},
108110
});
109111
return {
110-
text: [`Dreaming ${enabled ? "enabled" : "disabled"}.`, "", formatStatus(nextConfig)].join(
111-
"\n",
112-
),
112+
text: [
113+
`Dreaming ${enabled ? "enabled" : "disabled"}.`,
114+
"",
115+
formatStatus(committed.nextConfig),
116+
].join("\n"),
113117
};
114118
}
115119

‎extensions/nostr/index.ts‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,21 @@ export default defineBundledChannelEntry({
5252
},
5353
updateConfigProfile: async (_accountId: string, profile: unknown) => {
5454
const runtime = getNostrRuntime();
55-
const cfg = runtime.config.current() as OpenClawConfig;
5655

57-
const channels = (cfg.channels ?? {}) as Record<string, unknown>;
58-
const nostrConfig = (channels.nostr ?? {}) as Record<string, unknown>;
56+
await runtime.config.mutateConfigFile({
57+
afterWrite: { mode: "auto" },
58+
mutate: (draft) => {
59+
const channels = (draft.channels ?? {}) as Record<string, unknown>;
60+
const nostrConfig = (channels.nostr ?? {}) as Record<string, unknown>;
5961

60-
await runtime.config.replaceConfigFile({
61-
nextConfig: {
62-
...cfg,
63-
channels: {
62+
draft.channels = {
6463
...channels,
6564
nostr: {
6665
...nostrConfig,
6766
profile,
6867
},
69-
},
68+
};
7069
},
71-
afterWrite: { mode: "auto" },
7270
});
7371
},
7472
getAccountInfo: (accountId: string) => {

‎extensions/phone-control/index.ts‎

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,15 @@ async function disarmNow(params: {
230230
}
231231

232232
if (removed.length > 0 || restored.length > 0) {
233-
const next = patchConfigNodeLists(cfg, {
234-
allowCommands: uniqSorted([...allow]),
235-
denyCommands: uniqSorted([...deny]),
236-
});
237-
await api.runtime.config.replaceConfigFile({
238-
nextConfig: next,
233+
await api.runtime.config.mutateConfigFile({
239234
afterWrite: { mode: "auto" },
235+
mutate: (draft) => {
236+
const next = patchConfigNodeLists(draft, {
237+
allowCommands: uniqSorted([...allow]),
238+
denyCommands: uniqSorted([...deny]),
239+
});
240+
Object.assign(draft, next);
241+
},
240242
});
241243
}
242244
await writeArmState(statePath, null);
@@ -428,13 +430,15 @@ export default definePluginEntry({
428430
removedFromDeny.push(cmd);
429431
}
430432
}
431-
const next = patchConfigNodeLists(cfg, {
432-
allowCommands: uniqSorted([...allowSet]),
433-
denyCommands: uniqSorted([...denySet]),
434-
});
435-
await api.runtime.config.replaceConfigFile({
436-
nextConfig: next,
433+
await api.runtime.config.mutateConfigFile({
437434
afterWrite: { mode: "auto" },
435+
mutate: (draft) => {
436+
const next = patchConfigNodeLists(draft, {
437+
allowCommands: uniqSorted([...allowSet]),
438+
denyCommands: uniqSorted([...denySet]),
439+
});
440+
Object.assign(draft, next);
441+
},
438442
});
439443

440444
await writeArmState(statePath, {

‎extensions/slack/src/monitor/events/channels.ts‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { SlackEventMiddlewareArgs } from "@slack/bolt";
22
import { resolveChannelConfigWrites } from "openclaw/plugin-sdk/channel-config-writes";
3-
import { replaceConfigFile } from "openclaw/plugin-sdk/config-mutation";
3+
import { mutateConfigFile } from "openclaw/plugin-sdk/config-mutation";
44
import { formatErrorMessage } from "openclaw/plugin-sdk/error-runtime";
55
import { getRuntimeConfig } from "openclaw/plugin-sdk/runtime-config-snapshot";
66
import { danger, warn } from "openclaw/plugin-sdk/runtime-env";
@@ -145,9 +145,16 @@ export function registerSlackChannelEvents(params: {
145145
oldChannelId,
146146
newChannelId,
147147
});
148-
await replaceConfigFile({
149-
nextConfig: currentConfig,
148+
await mutateConfigFile({
150149
afterWrite: { mode: "auto" },
150+
mutate: (draft) => {
151+
migrateSlackChannelConfig({
152+
cfg: draft,
153+
accountId: ctx.accountId,
154+
oldChannelId,
155+
newChannelId,
156+
});
157+
},
151158
});
152159
ctx.runtime.log?.(warn("[slack] Channel config migrated and saved successfully."));
153160
} else if (migration.skippedExisting) {

‎extensions/talk-voice/index.ts‎

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,26 @@ export default definePluginEntry({
209209
return { text: `No voice found for ${hint}. Try: ${commandLabel} list` };
210210
}
211211

212-
const nextConfig = {
213-
...cfg,
214-
talk: {
215-
...cfg.talk,
216-
provider: providerId,
217-
providers: {
218-
...cfg.talk?.providers,
219-
[providerId]: {
220-
...cfg.talk?.providers?.[providerId],
221-
voiceId: chosen.id,
212+
await api.runtime.config.mutateConfigFile({
213+
afterWrite: { mode: "auto" },
214+
mutate: (draft) => {
215+
const nextConfig = {
216+
...draft,
217+
talk: {
218+
...draft.talk,
219+
provider: providerId,
220+
providers: {
221+
...draft.talk?.providers,
222+
[providerId]: {
223+
...draft.talk?.providers?.[providerId],
224+
voiceId: chosen.id,
225+
},
226+
},
227+
...(providerId === "elevenlabs" ? { voiceId: chosen.id } : {}),
222228
},
223-
},
224-
...(providerId === "elevenlabs" ? { voiceId: chosen.id } : {}),
229+
};
230+
Object.assign(draft, nextConfig);
225231
},
226-
};
227-
await api.runtime.config.replaceConfigFile({
228-
nextConfig,
229-
afterWrite: { mode: "auto" },
230232
});
231233

232234
const name = (chosen.name ?? "").trim() || "(unnamed)";

0 commit comments

Comments
 (0)