refactor(channels): dedupe hook and monitor execution paths

This commit is contained in:
Peter Steinberger
2026-02-22 21:18:53 +00:00
parent 06b0a60bef
commit 2081b3a3c4
19 changed files with 347 additions and 213 deletions

View File

@@ -0,0 +1,67 @@
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import {
readDiscordModelPickerRecentModels,
recordDiscordModelPickerRecentModel,
} from "./model-picker-preferences.js";
const tempDirs: string[] = [];
async function createStateEnv(): Promise<NodeJS.ProcessEnv> {
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-model-picker-"));
tempDirs.push(dir);
return { ...process.env, OPENCLAW_STATE_DIR: dir };
}
afterEach(async () => {
await Promise.all(
tempDirs.splice(0).map(async (dir) => {
await fs.rm(dir, { recursive: true, force: true });
}),
);
});
describe("discord model picker preferences", () => {
it("records recent models in recency order without duplicates", async () => {
const env = await createStateEnv();
const scope = { userId: "123" };
await recordDiscordModelPickerRecentModel({ env, scope, modelRef: "openai/gpt-4o" });
await recordDiscordModelPickerRecentModel({ env, scope, modelRef: "openai/gpt-4.1" });
await recordDiscordModelPickerRecentModel({ env, scope, modelRef: "openai/gpt-4o" });
const recent = await readDiscordModelPickerRecentModels({ env, scope });
expect(recent).toEqual(["openai/gpt-4o", "openai/gpt-4.1"]);
});
it("filters recent models using an allowlist", async () => {
const env = await createStateEnv();
const scope = { userId: "456" };
await recordDiscordModelPickerRecentModel({ env, scope, modelRef: "openai/gpt-4o" });
await recordDiscordModelPickerRecentModel({ env, scope, modelRef: "openai/gpt-4.1" });
const recent = await readDiscordModelPickerRecentModels({
env,
scope,
allowedModelRefs: new Set(["openai/gpt-4.1"]),
});
expect(recent).toEqual(["openai/gpt-4.1"]);
});
it("falls back to an empty store when the file is corrupt", async () => {
const env = await createStateEnv();
const stateDir = env.OPENCLAW_STATE_DIR as string;
const filePath = path.join(stateDir, "discord", "model-picker-preferences.json");
await fs.mkdir(path.dirname(filePath), { recursive: true });
await fs.writeFile(filePath, "{not-json", "utf-8");
const recent = await readDiscordModelPickerRecentModels({
env,
scope: { userId: "789" },
});
expect(recent).toEqual([]);
});
});

View File

@@ -1,11 +1,10 @@
import crypto from "node:crypto";
import fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { normalizeProviderId } from "../../agents/model-selection.js";
import { resolveStateDir } from "../../config/paths.js";
import { withFileLock } from "../../infra/file-lock.js";
import { resolveRequiredHomeDir } from "../../infra/home-dir.js";
import { readJsonFileWithFallback, writeJsonFileAtomically } from "../../plugin-sdk/json-store.js";
import { normalizeAccountId as normalizeSharedAccountId } from "../../routing/account-id.js";
const MODEL_PICKER_PREFERENCES_LOCK_OPTIONS = {
@@ -95,32 +94,6 @@ function sanitizeRecentModels(models: string[] | undefined, limit: number): stri
return deduped;
}
async function readJsonFileWithFallback<T>(
filePath: string,
fallback: T,
): Promise<{ value: T; exists: boolean }> {
try {
const raw = await fs.promises.readFile(filePath, "utf-8");
const parsed = JSON.parse(raw) as T;
return { value: parsed, exists: true };
} catch (err) {
const code = (err as { code?: string }).code;
if (code === "ENOENT") {
return { value: fallback, exists: false };
}
return { value: fallback, exists: false };
}
}
async function writeJsonFileAtomically(filePath: string, value: unknown): Promise<void> {
const dir = path.dirname(filePath);
await fs.promises.mkdir(dir, { recursive: true, mode: 0o700 });
const tmp = path.join(dir, `${path.basename(filePath)}.${crypto.randomUUID()}.tmp`);
await fs.promises.writeFile(tmp, `${JSON.stringify(value, null, 2)}\n`, "utf-8");
await fs.promises.chmod(tmp, 0o600);
await fs.promises.rename(tmp, filePath);
}
async function readPreferencesStore(filePath: string): Promise<ModelPickerPreferencesStore> {
const { value } = await readJsonFileWithFallback<ModelPickerPreferencesStore>(filePath, {
version: 1,