refactor: split config module
This commit is contained in:
83
src/config/defaults.ts
Normal file
83
src/config/defaults.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { resolveTalkApiKey } from "./talk.js";
|
||||
import type { ClawdisConfig } from "./types.js";
|
||||
|
||||
type WarnState = { warned: boolean };
|
||||
|
||||
let defaultWarnState: WarnState = { warned: false };
|
||||
|
||||
export type SessionDefaultsOptions = {
|
||||
warn?: (message: string) => void;
|
||||
warnState?: WarnState;
|
||||
};
|
||||
|
||||
function escapeRegExp(text: string): string {
|
||||
return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
||||
}
|
||||
|
||||
export function applyIdentityDefaults(cfg: ClawdisConfig): ClawdisConfig {
|
||||
const identity = cfg.identity;
|
||||
if (!identity) return cfg;
|
||||
|
||||
const name = identity.name?.trim();
|
||||
|
||||
const routing = cfg.routing ?? {};
|
||||
const groupChat = routing.groupChat ?? {};
|
||||
|
||||
let mutated = false;
|
||||
const next: ClawdisConfig = { ...cfg };
|
||||
|
||||
if (name && !groupChat.mentionPatterns) {
|
||||
const parts = name.split(/\s+/).filter(Boolean).map(escapeRegExp);
|
||||
const re = parts.length ? parts.join("\\s+") : escapeRegExp(name);
|
||||
const pattern = `\\b@?${re}\\b`;
|
||||
next.routing = {
|
||||
...(next.routing ?? routing),
|
||||
groupChat: { ...groupChat, mentionPatterns: [pattern] },
|
||||
};
|
||||
mutated = true;
|
||||
}
|
||||
|
||||
return mutated ? next : cfg;
|
||||
}
|
||||
|
||||
export function applySessionDefaults(
|
||||
cfg: ClawdisConfig,
|
||||
options: SessionDefaultsOptions = {},
|
||||
): ClawdisConfig {
|
||||
const session = cfg.session;
|
||||
if (!session || session.mainKey === undefined) return cfg;
|
||||
|
||||
const trimmed = session.mainKey.trim();
|
||||
const warn = options.warn ?? console.warn;
|
||||
const warnState = options.warnState ?? defaultWarnState;
|
||||
|
||||
const next: ClawdisConfig = {
|
||||
...cfg,
|
||||
session: { ...session, mainKey: "main" },
|
||||
};
|
||||
|
||||
if (trimmed && trimmed !== "main" && !warnState.warned) {
|
||||
warnState.warned = true;
|
||||
warn('session.mainKey is ignored; main session is always "main".');
|
||||
}
|
||||
|
||||
return next;
|
||||
}
|
||||
|
||||
export function applyTalkApiKey(config: ClawdisConfig): ClawdisConfig {
|
||||
const resolved = resolveTalkApiKey();
|
||||
if (!resolved) return config;
|
||||
const existing = config.talk?.apiKey?.trim();
|
||||
if (existing) return config;
|
||||
return {
|
||||
...config,
|
||||
talk: {
|
||||
...config.talk,
|
||||
apiKey: resolved,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function resetSessionDefaultsWarningForTests() {
|
||||
defaultWarnState = { warned: false };
|
||||
}
|
||||
Reference in New Issue
Block a user